如何从 App Delegate 检测场景变化?我有一个主菜单,它有一个使用 cocos2D 的菜单系统链接到第二页的按钮。
当用户按下按钮时,我将场景从 MenuScene 过渡到 GameScene。
是否可以从应用程序委托中检测到这种转换,以便我可以在场景转换时运行一些代码?
谢谢!
如何从 App Delegate 检测场景变化?我有一个主菜单,它有一个使用 cocos2D 的菜单系统链接到第二页的按钮。
当用户按下按钮时,我将场景从 MenuScene 过渡到 GameScene。
是否可以从应用程序委托中检测到这种转换,以便我可以在场景转换时运行一些代码?
谢谢!
您可以使用通知来通知场景已转换。在你的 appdelegate 的某个地方,监听通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingAfterTransition) name:@"sceneTransitioned" object:nil];
在您的 GameScene 的 onEnterTransitionDidFinish 方法中,您可以发布此通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"sceneTransitioned" object:nil];
解决方案可能是在每次更改场景时发布通知。
应用程序委托应注册该通知并在每次场景更改时接收通知。
你能使用已经内置在 CCNode 对象中的回调吗(CCScene 源自 CCNode)... 下面从 CCNode 复制粘贴来自 2.0 版,但我相信这些方法可以追溯到很久以前。任何 coco 'onSomething' 方法,如果你重写它,不要忘记 [super onSomething],否则你的里程会有所不同:)
/** Event that is called when the CCNode enters in the 'stage'.
If the CCNode enters the 'stage' with a transition, this event is called when the transition finishes.
If you override onEnterTransitionDidFinish, you shall
call [super onEnterTransitionDidFinish].
*/
-(void) onEnterTransitionDidFinish;
/** callback that is called every time the CCNode leaves the 'stage'.
If the CCNode leaves the 'stage' with a transition, this callback is called
when the transition starts.
*/
-(void) onExitTransitionDidStart;