0
votes

I have a home button on my scene which when pressed goes to the home menu. I use replaceScene to replace the current scene (Game Scene) with the HomeMenu Scene. For some reason the actions and sounds which are happening in the game scene are not stopped when I replace the scene. I have tried the following code but still when I am in the home menu I can hear the actions and sounds of the game scene playing.

// fired when the home menu is clicked! -(void) homeMenuClicked:(CCMenuItem *) item { NSLog(@"home menu clicked!");

CCScene *scene = [[CCDirector sharedDirector] runningScene]; [scene stopAllActions];

[self.layer stopAllActions];

[self unloadSoundEffects];

[[CCDirector sharedDirector] replaceScene:[CCSlideInLTransition transitionWithDuration:1.0 scene:[HomeScene scene]] ];

}

I must also add that the game layer also has a timer (NSTimer) object which starts in 2 seconds or something.

UPDATE 2:

Let me post some code! I think the problem is that when the player guess the correct answer the following method is invoked:

[self updateForCorrectAnswer];

Inside updateForCorrectAnswer I have a performSelector which is scheduled to fire in 6-7 seconds. I believe that performSelector is the culprit. If somehow can I stop that from being firing then the I think I will be fine.

    [self performSelector:@selector(refreshScore) withObject:nil afterDelay:7.0]; 
2
Are you sure your old scene (game layer) is being released ? Do you stop the timer in your game layer dealloc method ?Andrew
can you post more segment of your code? can't really tell much with the current codeCoke2Code
Updated the post which what I think is the problem!azamsharp

2 Answers

3
votes

You should not use NSTimer as cocos2d documents.

/* call scheduleUpdate in initializing */
[self scheduleUpdate];

It schedules update: method to be called every frame when this node is on the stage.

- (void)update:(ccTime)dt
{
    /* This method is automatically called every frame. */
}

scheduleUpdateWithPriority:, schedule:interval: are also available.

And why don't you use like this instead of performSelector:withObject:afterDelay.

[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:7], [CCCallFunc actionWithTarget:self selector:@selector(refreshScore)], nil]];
0
votes

If you use this method "[self performSelector:withObject:afterDelay:]", cocos2d will not manage it in this run loop. You should use it instead of the previous one:

[self schedule:interval:];

Then in your "homeMenuClicked:" method, just call this:

[self.layer unschedule:@selector(refreshScore)];

I haven't tried it but I think it'll be better.

For more information you can see the documentation here.