2010-05-27 8 views
2

저는 cocos2d를 사용하여 iPhone 용 전자 책 앱을 개발 중입니다. 150 개가 넘는 이미지를 사용합니다 (추측 하건데). 문제는 한 페이지에서 다른 페이지로 돌면서 이미지가 임의로 매달린다는 것입니다 ...iphone cocos2d의 메모리 관리

나는 이걸 [[TextureMgr sharedTextureMgr] removeAllTextures];도 시도했지만 헛된 것입니다. 나는 그 문제가 기억에 있다고 생각한다. 다음은 모든 페이지에 대한 내 코드입니다 :

-(id)init 
{ 
    if((self=[super init])) { 
     self.isTouchEnabled = YES; 
     [SimpleAudioEngine sharedEngine]; 
     NSLog(@"b4 cover"); 
     Sprite *bg1 = [Sprite spriteWithFile:@"a.jpg"]; 
     bg1.anchorPoint = CGPointZero; 
     [self addChild:bg1 z:-1]; 
     once = TRUE; 
     soundId = [[SimpleAudioEngine sharedEngine] playEffect:@".mp3"]; 
    } 
    return self; 
} 

-(void) transitionfront:(id) sender 
{ 
    [[SimpleAudioEngine sharedEngine] stopEffect:soundId]; 
    soundId1 = [[SimpleAudioEngine sharedEngine] playEffect:@"page_turn.mp3"]; 
    flip = [[Sprite spriteWithFile:@"a.jpg"] retain]; 
    [self addChild: flip z:1]; 
    [flip setPosition:ccp(160,240)]; 
    Animation* animation1 = [Animation animationWithName:@"Page1" delay:0.09]; 
    for(int i=1;i<4;i++) 
     [animation1 addFrameWithFilename: [NSString stringWithFormat:@".jpg", i]]; 

    id action = [Animate actionWithAnimation: animation1]; 
    //id action = [RepeatForever actionWithAction:[Animate actionWithAnimation: animation1]]; 
    [flip runAction:action]; 
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(moveforward) userInfo:nil repeats:NO]; 
} 

-(void) moveforward 
{ 
    [[SimpleAudioEngine sharedEngine] stopEffect:soundId1]; 
    [[Director sharedDirector] replaceScene: [ [Scene node] addChild: [nextpage node] z:0] ]; 
} 

-(void) transitionback:(id) sender 
{ 
    [[SimpleAudioEngine sharedEngine] stopEffect:soundId]; 
    soundId1 = [[SimpleAudioEngine sharedEngine] playEffect:@".mp3"]; 

    flip = [[Sprite spriteWithFile:@".jpg"] retain]; 
    [self addChild: flip z:1]; 
    [flip setPosition:ccp(160,240)]; 
    Animation* animation1 = [Animation animationWithName:@"Page1" delay:0.09]; 
    for(int i=3;i>0;i--) 
     [animation1 addFrameWithFilename: [NSString stringWithFormat:@".jpg", i]]; 

    id action = [Animate actionWithAnimation: animation1]; 
    //id action = [RepeatForever actionWithAction:[Animate actionWithAnimation: animation1]]; 
    [flip runAction:action]; 
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(movebackward) userInfo:nil repeats:NO]; 
} 

-(void) movebackward{ 
    //[[SimpleAudioEngine sharedEngine]stopEffect:@".mp3"]; 
    [[Director sharedDirector]replaceScene:[[Scene node]addChild:[b4page node] z:0]]; 
} 

-(void) glossary :(id) sender { 
    [[SimpleAudioEngine sharedEngine]stopEffect:soundId]; 
    [[Director sharedDirector]replaceScene:[[Scene node]addChild:[ node] z:0]]; 
} 

-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint cocosTouchPoint = [touch locationInView: [touch view]]; 
    CGPoint point = [[Director sharedDirector] convertToGL:cocosTouchPoint]; 

    NSLog(@"pointx: %f pointy:%f", point.x, point.y); 

    // Was a tab touched, if so, which one... 
    if (CGRectContainsPoint(CGRectMake(220, 0, 100, 70), point)) 
    { 
     if(once) 
     { 
      NSLog(@"enterred page1"); 

      [self transitionfront:nil]; 
      once = FALSE; 
     } 
    } 
    if (CGRectContainsPoint(CGRectMake(0,0,60,60), point)) 
    { 
     if(once) 
     { 
      NSLog(@"enterred cover"); 

      [self transitionback:nil]; 
      once = FALSE; 
     } 
    } 

    if (CGRectContainsPoint(CGRectMake(100, 15, 30, 30), point)) 
    { 
     if(once){ 
      [self glossary :nil]; 
      once = FALSE; 
     } 
    } 
    return kEventHandled; 
} 

-(void)playEffect:(NSString*)sound{ 
    if(effectPlayer!=nil){ 
     [effectPlayer release]; 
    } 
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:sound ofType:@"mp3"]]; 
    effectPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    [effectPlayer setDelegate:self]; 

    [effectPlayer play]; 
} 

-(void)stopEffect 
{ 
    [effectPlayer stop]; 
} 

-(void) dealloc{ 
    [super dealloc]; 
} 

는 PLS

이는 ERR입니다 코딩 나에게 정확한을 제공 할 ........ 좀 도와 않습니다는 .....

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: aesop.mp3)' 
2010-05-27 10:43:09.834 abc[276:20b] Stack: (
    11674715, 
    2476006971, 
    11758651, 
    11758490, 
    5126917, 
    660698, 
    660881, 
    661061, 
    131577, 
    448857, 
    120432, 
    153433, 
    630890, 
    23694899, 
    23603228, 
    23630005, 
    47120081, 
    11459456, 
    11455560, 
    47114125, 
    47114322, 
    23633923, 
    9928, 
    9814 
) 
+2

코드를 읽을 수있는 형식으로 포맷 할 수 있습니까? 에디터에는 작은 버튼이있어서 도움이됩니다. –

+0

코드를 명확하게 다시 포맷하십시오! 오류에 따라 키에 nil 값을 사전에 삽입하십시오. 아마도 그 가치가 왜 나오지 않는지 알기 시작하는 것이 가장 좋습니다. 귀하의 코드를 살펴 보니 기꺼이 하겠지만 먼저 다시 포맷하십시오. –

+1

나는 cocos2d를 사용한 적이 한번도 없지만 [[SimpleAudioEngine sharedEngine] playEffect : @ ".mp3"] 호출은 실제 mp3를 재생하는 것을 지정하지 않는 것 같습니다. 그게 맞습니까? – keno

답변

0

"헛된 것"은 무엇을 의미합니까?

필자도 비슷한 문제가 있었고, 모든 텍스처를 제거하려고 선택하면 메모리에있는 모든 이미지가 풀어져서 응용 프로그램이 CCSprites와 관련된 파일을 더 이상 찾을 수 없습니다.

: 당신이 형식 문자열을 craeting 때 INT를 참조하는 "내가 %"를 누락이 줄 또한

-(id) initWithFile:(NSString*)filename 
{ 
    NSAssert(filename!=nil, @"Invalid filename for sprite"); 
    CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: filename]; 
    if(texture) { 
     CGRect rect = CGRectZero; 
     rect.size = texture.contentSize; 
     return [self initWithTexture:texture rect:rect]; 
    } 

    [self release]; 
    return nil; 
} 

: CCSprite initWithFile 방법이 (가) sharedTextureCache에 텍스처를 추가하기 때문에

입니다 또한
[animation1 addFrameWithFilename: [NSString stringWithFormat:@"%i.jpg", i]]; 

예를 들어, 당신은 애니메이션을 사용하고 있지만 CCAnimation을 사용해야합니다

CCAnimation* anim = [CCAnimation animationWithFrames:frames delay:0.1f]; 

도움이 되나요?