是否可以检测到当前在场景中显示的是哪个 CCScene?我的游戏中有 2 个 CCScene,如果其中一个正在显示,我希望执行某个操作。
还有一个快速相关的问题,如果我想检查 CCMenu 目前是否没有显示,我会做类似的事情
if (!menu) {
//Menu is not showing currently
}
说到Cocos2D,我有点菜鸟,所以请原谅我:)
谢谢!
是否可以检测到当前在场景中显示的是哪个 CCScene?我的游戏中有 2 个 CCScene,如果其中一个正在显示,我希望执行某个操作。
还有一个快速相关的问题,如果我想检查 CCMenu 目前是否没有显示,我会做类似的事情
if (!menu) {
//Menu is not showing currently
}
说到Cocos2D,我有点菜鸟,所以请原谅我:)
谢谢!
您可以使用 CCDirector 来判断正在运行的场景。
[[CCDirector sharedDirector] runningScene];
至于菜单是否显示。您必须与菜单的父级确认。如果您的 CCLayer 所在的父级,那么您可以通过
// assume menu is set up to have tag kMenuTag
CCMenu * menu = [self getChildByTag:kMenuTag];
如果菜单是其他节点的子节点,您可以通过类似的方法获取父节点并获取对菜单的引用。
如果menu == nil
,则不显示。
更新
在 cocos2d 中,不鼓励您保留对所有精灵的引用,而是应该为每个节点提供一个唯一的标签并使用它来引用它。为了实现您的第一个目标,您可以在您的 2 个各自的 CCLayer 类中为您的场景添加一个标签。
您可以在名为 Tags.h 的文件中的枚举中设置您的唯一标签,然后将其导入需要访问您的标签的任何类
示例标签.h
enum {
kScene1Tag = 0,
kScene2Tag = 1,
kMenuTag = 2};
然后在你的图层类
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
scene.tag = kScene1Tag;
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
现在,当您抓取当前场景时,您可以检查标签
int currentSceneTag = [[CCDirector sharedDirector] runningScene].tag;
if (currentSceneTag == kScene1Tag) {
} else if (currentSceneTag == kScene2Tag) {
}
tag
属性来自CCNode
which 是CCLayer
, CCScene
, CCSprite
, CCMenu
...的基类
这是如何找出正在运行的场景
if ([CCDirector sharedDirector].runningScene == yourScene1) {
// your scene 1 is showing
} else {
// your scene 2 is showing
}
并找出一个节点是否是正在运行的场景的子节点
BOOL isShowing = NO;
CCNode *node = yourMenu;
while (node != nil) {
if (node == [CCDirector sharedDirector].runningScene) {
isShowing = YES;
break;
} else {
node = node.parent;
}
}
if (isShowing) {
// your menu is in the display hierarchy
}