2010-05-27 94 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]; 
} 

就請幫我........你給我一個確切的編碼

這是犯錯.....

*** 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

你能把你的代碼格式化成可讀的東西嗎?編輯器上有一個小按鈕來幫助你。 – 2010-05-27 16:39:08

+0

絕對重新格式化您的代碼!根據這個錯誤,你的密鑰插入一個字典的零值。可能最好開始,看看爲什麼價值是零。我很樂意瀏覽你的代碼,但請首先重新格式化。 – 2010-05-27 21:43:12

+1

我從來沒有使用過cocos2d,但是你對[[SimpleAudioEngine sharedEngine] playEffect:@「。mp3」]的調用似乎並未指定要播放的實際mp3。是對的嗎? – keno 2012-04-25 05:51:17

回答

0

你是什麼意思爲「走向徒勞」?

我有一個類似的問題,當你選擇removeAllTextures時,你會釋放內存中的所有圖像,然後你的應用程序將不會再找到與CCSprites相關的文件。

這是因爲CCSprite initWithFile方法添加紋理到sharedTextureCache:

-(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; 
} 
在這一行

而且你缺少「%i」的craeting與格式字符串時,指的是INT:

[animation1 addFrameWithFilename: [NSString stringWithFormat:@"%i.jpg", i]]; 

而且使用的是動畫,但你應該使用CCAnimation,如:

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

這有幫助嗎?