【问题标题】:COCOS 2D iPhone - Parsing variable between scenes and using in layerCOCOS 2D iPhone - 场景之间解析变量并在图层中使用
【发布时间】:2013-05-30 13:49:19
【问题描述】:

我有用变量调用新场景的代码:

    [[CCDirector sharedDirector] replaceScene:[GameScene sceneWithParam:item.tag]];

在 GameScene.h 中

@interface GameScene : CCLayer {
}



+(id) sceneWithParam:(int)nvl;
@end

GameScene.m

+(id) sceneWithParam:(int)nvl
{
    CCScene *scene = [CCScene node];

    GameScene *layer = [GameScene node];

    [scene addChild: layer];

    return scene;

}


-(id) init
{

    if( (self=[super init] )) {



    }
    return self;
}

我不能在if( (self=[super init] )) { 中使用变量nil 我已经尝试设置属性testtest = nvl; inside +(id) sceneWithParam:(int)nvl;,但这是不可能的。

【问题讨论】:

    标签: iphone ios cocos2d-iphone


    【解决方案1】:

    是的,不能在静态方法中使用动态参数。如果你想用参数创建你的对象,你必须创建一个 init 方法,接收你需要的参数。例如:

    -(id) initWithYourParam:(id)param
    {
        if ( (self=[self init]) ) {
            self.propertyParam = param;
        }
        return self;
    }
    

    【讨论】:

    • 但是如何使用层中的参数 (if( (self=[super init] )) { ) ?
    • 找到代码调用你的init方法的地方,我猜它是[GameScene node];。现在您的派生类中没有此方法,因此它需要父类的方法。您需要在您的 GameScene 类中实现 node (或任何调用 GameScene 的 init 的方法)并使其调用 initWithParam 而不是随意 初始化
    • 它有效。我使用了 [[GameScene alloc] initWithParam:nvl]。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多