开发者

Cocos2d-iPhone: "update" called after dealloc

So I have a subclass of a CCSprite object, and in its init method, I call:

[self scheduleUpdate]

I later release this object from its parent CCNode like so:

[self removeChild:sprite cleanup:YES];

In addition, I call [self unscheduleUpdate] in the sprite's dealloc method.

However, I'm getting a bad memory access, so it appears that the update method is still attempted after the object is released (I've narrowed it down to this, as it works perfectly if I comment out the [self scheduleUpdate] line.

开发者_C百科

Any ideas?


Found this post in an attempt to ask the same question. I tried unschedule update (within my init method as well) with no luck, but then realized that by moving the [self unscheduleUpdate]; to the actual update method (which is running continuously, unlike the init method) based on a condition it worked!

So, for those looking for copy paste, here's a progress bar example that I'm implementing from http://www.ccsprite.com/cocos2d/using-ccprogresstimer-cocos2d-example.html#HCB_comment_box

 -(id) init
    {
    //initialize progress bar, make sure to add a file named green_health_bar.png to your
    //resource folder
        timer = [CCProgressTimer progressWithFile:@"green_health_bar.png"];

        //set the progress bar type to horizontal from left to right
        timer.type = kCCProgressTimerTypeHorizontalBarRL;

        //initialize the progress bar to zero
        timer.percentage = 0;

        //add the CCProgressTimer to our layer and set its position
        [self addChild:timer z:1 tag:20];
        [timer setPosition:ccp(100, 280)];
        [self scheduleUpdate];
    }

and in your update method:

-(void)update:(ccTime)dt

{

    //get progress bar
    CCNode* node = [self getChildByTag:20];

    timer.percentage += dt * 10;
    if (timer.percentage >= 100)
    {
        [self gameOver]; //used to stop parallax and show gameover menu
        [self unscheduleUpdate];
    }
}

I usually don't allow forum reply emails, but feel free to ask questions via @russ152!


Hmm.. try not to use scheduleUpdate? I tried looking for self unscheduleUpdate but there is not such function in a CCNode.. You can try [self unscheduleAllselectors], which stops all selectors of the object , including the update selector, if you are not using the object anymore.. Or use custom selectors instead..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜