Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 游戏场景共享游戏场景与游戏场景节点_Objective C_Xcode_Cocos2d Iphone_Singleton - Fatal编程技术网

Objective c 游戏场景共享游戏场景与游戏场景节点

Objective c 游戏场景共享游戏场景与游戏场景节点,objective-c,xcode,cocos2d-iphone,singleton,Objective C,Xcode,Cocos2d Iphone,Singleton,我正在将一些游戏引擎移植到另一个游戏代码中,我有两个问题 守则: -(void) checkForBulletCollisions { Enemy* enemy; CCARRAY_FOREACH([batch children], enemy) { if (enemy.visible) {

我正在将一些游戏引擎移植到另一个游戏代码中,我有两个问题

守则:

-(void) checkForBulletCollisions
        {
                Enemy* enemy;
                CCARRAY_FOREACH([batch children], enemy)
                {
                if (enemy.visible)
                {
                        BulletCache* bulletCache = [[GameScene sharedGameScene] bulletCache];
                        //etc etc
我将此代码移植到的项目中的游戏场景与上述代码中的游戏场景不同,它不是一个单一的场景

以下是游戏场景的界面代码:

@interface GameScene : CCScene
    +(void) newGame:(NSString*)levelFile;
    -(void) reloadGame:(NSString*)levelFile;
@end
在移植代码顶行时,我将代码顶行替换为:

BulletCache* bulletCache = [[GameScene node] bulletCache];

我走对了吗?

正如hiepnd所说的那样-node方法创建一个新的自动释放实例。然而,您正在移植的代码正在调用单例。我将假设singleton是“当前场景”——因此,移植的任务是在节点层次结构中找到顶层节点——确认它确实属于GameSecene类,然后从中检索bulletCache的yur引用

当前场景可以从导演处检索,如下所示:

CCScene *currentScene = [[CCDirector sharedDirector] runningScene];
GameScene *gameScene = nil;
if ([currentScene isKindOfClass:[GameScene class]]) gameScene = currentScene;
BulletCache *bulletCache = [gameScene bulletCache];

[GameSecene node]返回一个新的GameSecene实例,因此您将无法获得预期的结果。具有checkForBulletCollisions方法的类应具有对GameSecene实例的引用,以便您可以调用bulletCache=[TheGameSeceneInstance bulletCache];此外,GameSecene不是单身汉的理想人选。谢谢你的提示!顺便说一句,在我的项目中,BulletCache的顶部节点是LevelObject类,它本身是CCNode的一个子类,而另一个项目的代码在其游戏场景中有子弹的访问器,这就是为什么它调用GameSecene singleton@hiepend我同意你的观点,这就是为什么我要将Bullet/BulletCache类作为预先存在的LevelObject类的子类,它是CCNode的子类