質問

In Cocos2d i use to get my current running scene through the code:

CCScene *runningScene = [[CCDirector sharedDirector] runningScene];
    if ([runningScene isKindOfClass:[GameScene class]])

Is there any similar method to get this on cocos2d-x also???

I am using:

CCScene *scene = (CCScene *)CCDirector::sharedDirector()->getRunningScene();

but how to compare it with current scene??

役に立ちましたか?

解決

You can use dynamic cast which returns pointer of given type or returns NULL value.

CCScene *scene = CCDirector::sharedDirector()->getRunningScene();    
GameScene* gameScene = dynamic_cast<GameScene*>(scene);    
if(gameScene != NULL)
{
   // scene is type of GameScene
}

他のヒント

You can use dynamic_cast, but you must pay attention to the difference between CCLayer and CCScene. GameScene is type CCLayer, I guess. So if you write the following code, you can not get the hopeful result.

GameScene *gameScene = dynamic_cast<GameScene*>(scene);

scene is type CCScene, and GameScene is type CCLayer. You must do as followings.

**

CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
CCLayer *layer = (CCLayer*)(scene->getChildren()->objectAtIndex(0));
GameScene *gameScene = dynamic_cast<GameScene*>(layer);

**

if GameScene is inherited from other customized Layer, e.g., MyLayer, you should change CCLayer with MyLayer.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top