0

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

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

在 GameScene.h 中

@interface GameScene : CCLayer {
}



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

游戏场景.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;
}

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

4

1 回答 1

1

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

-(id) initWithYourParam:(id)param
{
    if ( (self=[self init]) ) {
        self.propertyParam = param;
    }
    return self;
}
于 2013-05-30T14:04:25.353 回答