我的场景中有一个主页按钮,按下后会进入主菜单。我使用replaceScene将当前场景(游戏场景)替换为HomeMenu场景。由于某种原因,当我替换场景时,游戏场景中发生的动作和声音不会停止。我尝试了以下代码,但当我在主菜单中时,我可以听到游戏场景播放的动作和声音。
//在主菜单中被触发 点击! - (void)homeMenuClicked:(CCMenuItem *)item {NSLog(@“主菜单点击!”);
CCScene * scene = [[CCDirector sharedDirector] runningScene]; [现场 stopAllActions];
[self.layer stopAllActions];
[self unloadSoundEffects];
[[CCDirector sharedDirector] replaceScene:[CCSlideInLTransition transitionWithDuration:1.0 场景:[HomeScene场景]]];
}
我还必须补充一点,游戏层还有一个定时器(NSTimer)对象,该对象在2秒内启动。
更新2:
让我发布一些代码!我认为问题在于当玩家猜出正确的答案时,会调用以下方法:
[self updateForCorrectAnswer];
inside updateForCorrectAnswer我有一个performSelector,计划在6-7秒内触发。我相信performSelector是罪魁祸首。如果我能以某种方式阻止它被解雇那么我认为我会没事的。
[self performSelector:@selector(refreshScore) withObject:nil afterDelay:7.0];
答案 0 :(得分:3)
您不应将NSTimer用作cocos2d文档。
/* call scheduleUpdate in initializing */
[self scheduleUpdate];
它调度update:当该节点在舞台上时每帧被调用的方法。
- (void)update:(ccTime)dt
{
/* This method is automatically called every frame. */
}
scheduleUpdateWithPriority:,schedule:interval:也可用。
为什么不使用这样的代替performSelector:withObject:afterDelay。
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:7], [CCCallFunc actionWithTarget:self selector:@selector(refreshScore)], nil]];
答案 1 :(得分:0)
如果您使用此方法“[self performSelector:withObject:afterDelay:]”,cocos2d将不会在此运行循环中管理它。您应该使用它而不是前一个:
[self schedule:interval:];
然后在你的“homeMenuClicked:”方法中,只需调用:
[self.layer unschedule:@selector(refreshScore)];
我没试过,但我认为它会更好。
有关详细信息,请参阅文档here。