如何在Cocos2d-x中获取运行场景类类型
在Cocos2d中,我使用以下代码获取当前正在运行的场景:如何在Cocos2d-x中获取运行场景类类型,cocos2d-x,appdelegate,Cocos2d X,Appdelegate,在Cocos2d中,我使用以下代码获取当前正在运行的场景: CCScene *runningScene = [[CCDirector sharedDirector] runningScene]; if ([runningScene isKindOfClass:[GameScene class]]) 有没有类似的方法也可以在cocos2d-x上获取此信息 我正在使用: CCScene *scene = (CCScene *)CCDirector::sharedDirector()->
CCScene *runningScene = [[CCDirector sharedDirector] runningScene];
if ([runningScene isKindOfClass:[GameScene class]])
有没有类似的方法也可以在cocos2d-x上获取此信息
我正在使用:
CCScene *scene = (CCScene *)CCDirector::sharedDirector()->getRunningScene();
但如何将其与当前场景进行比较???您可以使用
动态强制转换
,它返回给定类型的指针或返回空值
CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
GameScene* gameScene = dynamic_cast<GameScene*>(scene);
if(gameScene != NULL)
{
// scene is type of GameScene
}
CCScene*scene=CCDirector::sharedDirector()->getRunningScene();
游戏场景*游戏场景=动态施法(场景);
if(游戏场景!=NULL)
{
//场景是游戏场景的一种
}
您可以使用动态强制转换
返回给定类型的指针或返回空值
CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
GameScene* gameScene = dynamic_cast<GameScene*>(scene);
if(gameScene != NULL)
{
// scene is type of GameScene
}
CCScene*scene=CCDirector::sharedDirector()->getRunningScene();
游戏场景*游戏场景=动态施法(场景);
if(游戏场景!=NULL)
{
//场景是游戏场景的一种
}
您可以使用动态_cast,但必须注意CCLayer和CCScene之间的区别。
我猜游戏场景是CCLayer类型的。
因此,如果您编写以下代码,则无法获得希望的结果
GameScene *gameScene = dynamic_cast<GameScene*>(scene);
**
如果
游戏场景
是从其他自定义层继承的,例如MyLayer
,则应使用MyLayer
更改CCLayer
,您可以使用dynamic_cast,但必须注意CCLayer和CCScene之间的差异。
我猜游戏场景是CCLayer类型的。
因此,如果您编写以下代码,则无法获得希望的结果
GameScene *gameScene = dynamic_cast<GameScene*>(scene);
**
如果
游戏场景
是从其他自定义层继承的,例如MyLayer
,您应该用MyLayer
更改CCLayer
,我这里有一个奇怪的情况。我完全按照你的建议去做,这就是我需要的场景。断言失败,因为dynamic_cast
将结果转换为NULL
指针。我在安卓上,RTTI标志打开。我不知道出了什么事,这不应该发生。确保在条件出错时始终执行assert。如果可能,共享代码块。同意,不应该发生这种情况。这是我的代码:DecorationScene*DecorationScene=dynamic_cast(Director::getInstance()->getRunningScene());断言(decorationScene!=NULL&“仅从装饰场景调用弹出窗口”)代码>@Narek您找到问题的解决方案了吗?我也总是只得到一个空指针。我这里有一个奇怪的情况。我完全按照你的建议去做,这就是我需要的场景。断言失败,因为dynamic_cast
将结果转换为NULL
指针。我在安卓上,RTTI标志打开。我不知道出了什么事,这不应该发生。确保在条件出错时始终执行assert。如果可能,共享代码块。同意,不应该发生这种情况。这是我的代码:DecorationScene*DecorationScene=dynamic_cast(Director::getInstance()->getRunningScene());断言(decorationScene!=NULL&“仅从装饰场景调用弹出窗口”)代码>@Narek您找到问题的解决方案了吗?我也总是只得到一个空指针。