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
.