微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将变量从一个场景传递到另一个场景-Cocos2D

如何解决将变量从一个场景传递到另一个场景-Cocos2D

| 我的游戏中有一个关卡场景,我要在其中选择关卡。在此场景中,我将显示cclabelTTF中选择的级别。现在,我想将此标签显示的值传递到我的主场景。我这样做如下:
HelloWorld *hello=[HelloWorld getInstance];  //HelloWorld is main scene  

hello.strLevel=[lblLevel string];  //strLevel is Nsstring to which I am passing the label text  

[[CCDirector sharedDirector]replaceScene:[HelloWorld node]];  
在我的HelloWorld场景中,我正在使用Singleton共享在Level场景中使用的标签值。
//HelloWorld.h  


@interface HelloWorld : CCColorLayer  

{  

Nsstring *strLevel;  

}  

@property(nonAtomic,retain)Nsstring *strLevel;  

+(HelloWorld*)getInstance;  

HelloWorld.mm  

@implementation HelloWorld  

@synthesize strLevel;  

static HelloWorld *instance=nil;  

__________________________  

//Some code  

__________________________  


+(HelloWorld*)getInstance  

{  

if(instance==nil)  

{  

instance=[HelloWorld new];  

} 

return instance;  

}  
但是,这不起作用。控制权一到达
instance=[HelloWorld new];  
init()被调用。那么为何不。但是,当控件在我传递值的行返回到Level场景时,什么也没有发生,并且HelloWorld将strLevel的值显示为null。 我知道单例是比AppDelegate传递值更好的方法。但是我无法做到这一点,有人可以纠正我吗? 谢谢     

解决方法

使用单例。我的Objective-C单例应该是什么样?这是对obj-c中的单例的很好的讨论。祝好运 [编辑]
 HelloWorld *hello=[HelloWorld getInstance]; //HelloWorld is main scene  
 hello.strLevel=[lblLevel string]; //strLevel is NSString to which I am passing the label text
 [[CCDirector sharedDirector]replaceScene:[HelloWorld node]];
您传递给replaceScene的HelloWorld实例与 您将单例实例传递给的HelloWorld * hello。这就是为什么其中没有strLevel值的原因。但是,strLevel值放置在HelloWorld单例中。尝试
NSLog(@\"%@\",[[HelloWorld getInstance] strLevel]); //somewhere in the code
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。