我有使用变量调用新场景的代码:
[[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/