简体   繁体   中英

Memory management in iphone cocos2d

I am developing an ebook app for the iPhone using cocos2d. I use over 150 images(I guess). The problem is, while turning from one page to another, images get hanged randomly......

I tried this also [[TextureMgr sharedTextureMgr] removeAllTextures]; but in vain. I guess the the problem is with the memory. Below is my code for all pages:

-(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];
}

do pls help me........ do give me a exact coding

this is the 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
)

What do you mean for "went vain"?

I had a similar issue and when you choose to removeAllTextures you loose all the images that you have in memory and then your App won't find anymore the file related to the CCSprites.

This is because CCSprite initWithFile method adds the texture to the 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;
}

Also in this line you are missing "%i" to refer to the int when craeting the String with format:

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

Also you are using Animation but you should use CCAnimation, eg:

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

Does this help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM