iphone - COCOS 2D iPhone - 解析场景之间的变量并在图层中使用

标签 iphone ios cocos2d-iphone

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

    [[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;
}

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

最佳答案

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

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

关于iphone - COCOS 2D iPhone - 解析场景之间的变量并在图层中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16838038/

相关文章:

ios - 如何将 <1100> 或 <2200> 写入我的 CBPeripheral 上的特征值?

ios - Box2D 缩放说明

iphone - iOS 本地化, label with number and word together

javascript - WKWebView invalidaccesserror dom异常15底层对象不支持参数或操作

iphone - 是时候为 Apple 不可知论者学习 Objective-C/Cocoa

objective-c - 我的谷歌地图 iOS 应用中的缩放级别受到限制

iphone - 在 xcode 4.2 中我应该选择哪个 C/C++/Objective C 编译器?

iphone - 在 Google App Engine Python API 中获取原始发布数据

ios - iOS 10 中的调用阻止功能

iphone - MKMapview 影响 UINavigationBar 和 TabBar 外观