2011-02-28 59 views
0

我的場景中有一個主頁按鈕,當它按下時會轉到主菜單。我使用replaceScene來用HomeMenu場景替換當前場景(Game Scene)。出於某種原因,當我替換場景時,遊戲場景中發生的動作和聲音不會停止。我嘗試了下面的代碼,但仍然在主菜單中時,我可以聽到遊戲場景的動作和聲音。替換場景並停止所有動畫和聲音

//當主菜單是 點擊了! (無效)homeMenuClicked:(CCMenuItem *)item {NSLog(@「home menu clicked!」);

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

[self.layer stopAllActions];

[self unloadSoundEffects];

[[CCDirector sharedDirector] replaceScene:[CCSlideInLTransition transitionWithDuration:1.0 場景:場景HomeScene]]];

}

我還必須補充的是,遊戲層還具有開始2秒或東西的定時器(的NSTimer)對象。

更新2:

讓我發表一些代碼吧!我認爲問題是,當玩家猜出正確答案被調用下面的方法:

[self updateForCorrectAnswer]; 

裏面updateForCorrectAnswer我有計劃在6-7秒內火performSelector。我相信performSelector是罪魁禍首。如果不知何故,我可以阻止它被解僱,那麼我認爲我會沒事的。

[self performSelector:@selector(refreshScore) withObject:nil afterDelay:7.0]; 
+0

您確定您的舊場景(遊戲層)正在發佈嗎?你在遊戲層dealloc方法中停止計時器嗎? – Andrew 2011-02-28 22:49:12

+0

你可以發佈更多的代碼段嗎?用當前的代碼不能真正分辨 – Treon 2011-03-01 02:43:28

+0

更新了我認爲是問題的帖子! – azamsharp 2011-03-01 16:36:00

回答

3

您不應該使用NSTimer作爲cocos2d文檔。

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

它調度更新:當該節點在舞臺上時調用每幀的方法。

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

如果您使用此方法 「[自performSelector:withObject:afterDelay:]」 的cocos2d不會在此運行循環管理。你應該用它代替前一個:

[self schedule:interval:]; 

然後在您的「homeMenuClicked:」方法,只需撥打這個:

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

我還沒有嘗試過,但我認爲這將是更好。

欲瞭解更多信息,你可以看到文檔here