简体   繁体   English

Cocos2d访问场景的属性和方法

[英]Cocos2d access scene properties and methods

I'm trying to include a pause feature for my game. 我正在尝试为我的游戏添加暂停功能。 So far running pause on the [CCDirector sharedDirector] has been good enough when run through my pauseGame method (which incorporates a BOOL to tell if game is paused, etc.) 到目前为止,通过我的pauseGame方法运行时,在[CCDirector sharedDirector]上的运行pause已经足够好(该合并了BOOL以告知游戏是否已暂停等)。

However, I noticed that if I go back to the homescreen on my device, my application delegate will automatically run pause and resume on the sharedDirector . 但是,我注意到,如果我返回设备上的主屏幕,则我的应用程序委托将自动在sharedDirector上运行pauseresume

Ideally, I would like access my active scene/layer's so I can run my own pause method. 理想情况下,我想访问活动场景/图层,以便可以运行自己的暂停方法。

How can I: 1) check if the current scene is my game scene 2) access the game scene's pause property, and run the pauseGame method on it? 如何:1)检查当前场景是否是我的游戏场景2)访问游戏场景的pause属性,并在其上运行pauseGame方法?

Any help appreciated. 任何帮助表示赞赏。 Thanks 谢谢

The running scene is [CCDirector sharedDirector].runningScene 正在运行的场景是[CCDirector sharedDirector].runningScene

If you call [scene pauseSchedulerAndActions]; 如果您调用[scene pauseSchedulerAndActions]; on the current scene this is will not be resumed when going back and forth on the home screen. 在当前场景中,在主屏幕上来回移动时将不会恢复。

If you need to pause ALL nodes in the hierarchy, here is a method to extend CCNode 如果需要暂停层次结构中的所有节点,这是扩展CCNode的方法

-(void)recursivePauseSchedulerAndActions {
    [self pauseSchedulerAndActions];
    CCNode *child;
    CCARRAY_FOREACH(children_, child) {
        [child recursivePauseSchedulerAndActions];
    }
}
-(void)recursiveResumeSchedulerAndActions {
    [self resumeSchedulerAndActions];
    CCNode *child;
    CCARRAY_FOREACH(children_, child) {
        [child recursiveResumeSchedulerAndActions];
    }
}

I use a different approach for pausing a game. 我使用另一种方法暂停游戏。

I usually have a game scene with game layers. 我通常会有一个带有游戏图层的游戏场景。 To pause the game I add a new layer (covering the entire screen) and when I pause the game I display this layer. 为了暂停游戏,我添加了一个新层(覆盖整个屏幕),当我暂停游戏时,我显示了这一层。 This way you can stop touching events for the game layer and restore them when resuming the game (and removing the pause layer from the scene) 这样,您可以停止触摸游戏图层的事件并在恢复游戏时恢复它们(并从场景中删除暂停图层)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM