Cocos2d iphone 访问其他层中的对象(cocos2d)

Cocos2d iphone 访问其他层中的对象(cocos2d),cocos2d-iphone,ccsprite,Cocos2d Iphone,Ccsprite,我在玩一个操纵杆,在一层中移动一个精灵。现在,根据最佳实践,操纵杆和精灵必须位于同一场景的不同层中。我已经成功地将它们分开了,但是我现在完全没有任何线索知道如何将操纵杆命令从一层传递到另一层?推荐的方法是什么 场面 游戏层 雪碧 游戏控制层 操纵杆 操纵操纵杆时,我需要将此信息传递给游戏玩家来控制精灵。好吧,我得到了一本很棒的Cocos2d书,由罗德·斯特劳戈和雷·温德里奇写,书名为“学习CoCoCoS2D”。在他们的书中,他们使用初始设置实现了一个游戏,其中包括一个操纵杆。Game

我在玩一个操纵杆,在一层中移动一个精灵。现在,根据最佳实践,操纵杆和精灵必须位于同一场景的不同层中。我已经成功地将它们分开了,但是我现在完全没有任何线索知道如何将操纵杆命令从一层传递到另一层?推荐的方法是什么

场面

  • 游戏层
    • 雪碧
  • 游戏控制层
    • 操纵杆

操纵操纵杆时,我需要将此信息传递给游戏玩家来控制精灵。

好吧,我得到了一本很棒的Cocos2d书,由罗德·斯特劳戈和雷·温德里奇写,书名为“学习CoCoCoS2D”。在他们的书中,他们使用初始设置实现了一个游戏,其中包括一个操纵杆。GamePlayLayer包含操纵杆和英雄精灵。(见本书第40页)

我不相信他们会在书中使用坏习惯,因为他们很有天赋

话虽如此,如果您希望在不同的层上实现它们,我有可能的解决方案:

GameScene
|
|__>GameLayer
|
|__>ControlLayer
这是你的基本设置。但是,凭直觉,控制层的目的是什么?控制游戏层的内容!因此,我建议您在ControlLayer中保留一个(弱)对GameLayer的引用。这样,使用一个简单的

@property (nonatomic, assign) CCSprite* hero;
你现在可以从操控者那里接触英雄了

额外费用(如需要):

尽管我只是建议这样做,但我不会在游戏中使用:)

我通常做的是:

使游戏场景类成为“半单体”。(我是从Itterheim(又名游戏恐怖,Kobold2d出版商……等)的“学习iPhone和iPad游戏开发”中学到这个方法的。)

然后,在控制层内,我将调用GameSecene对象:

[[GameScene sharedScene] doSomethingToTheGameLayer];
是的,游戏场景有一些简单化的方法,这些方法仅仅依赖于游戏层中控件需要更新的内容


编辑: 实现半单例模式,如Itterheim在其书中所述

但是,什么是半单身

它具有单例模式的属性:您可以使用静态调用从任何地方访问对象实例

[GameScene sharedScene];
但是,单例对象通常会在第一次创建后一直保留到应用程序的生命周期结束。在半单例模式中,情况并非如此

创建实例后,在销毁旧实例之前不能再创建另一个实例,但一旦处理完该实例,就可以销毁它(dealloc)。必要时创建另一个实例

概述: 半单体:从中创建多个对象,但在任何给定时间只能创建一个。只有在销毁旧对象后才能重新创建

实施:

当然,与处理任何单例类一样,首先声明一个与类类型相同的静态变量:

//In GameScene.m
static GameScene* instance = nil;

@implementation

//Here we have the static method to access the instance:
+(GameScene*)sharedScene {
    //you must have created one before trying to access it "Globally".
    ///Typically, created where you transition the GameScene using CCDirector's replaceScene.
    NSAssert(instance != nil, @"GameScene not instantiated!!");
    return instance;
}

-(id)init {
    if((self = [super init])) {
        //Make sure you don't have another instance of the class created
        //NOTE: Possible race condition that I wouldn't worry about, since it should never happen.
        NSAssert(instance == nil, @"Another instance is already created!!");
        instance = self;

        ...
    }
    return self;
}

//Don't forget!! Dealloc!!
- (void)dealloc {
    //the only instance we had is dealloc'd! We are back to zero. Can create a new one later! :D
    instance = nil;
    [super dealloc];
}
编辑2: 因此,时间表:

CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...
[[GameScene sharedScene] doSomething];
...
[[CCDirector sharedDirector] replaceScene:otherScene];
//After the new scene replaces GameScene, dealloc will be called, implicitly. Making instance = nil;
instance = nil;
[super dealloc];
...
//Loop again
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...

谢谢你的全面回复!我已经跟随了《罗德·斯特劳戈》这本书并取得了相当大的进步。然而,其他作者如《斯特芬·伊特海姆》(你也提到了他)我还有其他的想法。我还在学习Cocos2d和Objective-C,我只想探索这两种方法。你能给我一些关于半单例模式的见解吗?因为它听起来比保留一个弱引用更合理,因为我提倡完全封装;-)
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...
[[GameScene sharedScene] doSomething];
...
[[CCDirector sharedDirector] replaceScene:otherScene];
//After the new scene replaces GameScene, dealloc will be called, implicitly. Making instance = nil;
instance = nil;
[super dealloc];
...
//Loop again
CCScene* scene = [GameScene node];
[[CCDirector sharedDirector] replaceScene:scene];
...