1

I have a level scene in my game in which I am selecting the level. In this scene I am displaying the level selected in a CCLabelTTF. Now I want to pass the value displayed on this label to my main scene. I am doing this as follows:

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]];  

In my HelloWorld scene I am using singleton to share the value of label used in Level scene.

//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;  

}  

However this isn't working.As soon as control reaches

instance=[HelloWorld new];  

init() is called. And why not. However when the control reaches back to Level scene at the line where I am passing the value, nothing happens and HelloWorld shows the value null for strLevel.

I know singleton is a better way to pass values than AppDelegate. But I am unable to do so.Can someone correct me?

Thanks

Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

2

Use singleton. What should my Objective-C singleton look like? this is a good discussion on the singleton in obj-c. good luck

[EDIT]

 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]];

The HelloWorld instance that you're passing to the replaceScene is not the same as the HelloWorld *hello you passed the singleton instance to. That's why there is no strLevel value in it. The strLevel value is placed in your HelloWorld singleton though. Try

NSLog(@"%@",[[HelloWorld getInstance] strLevel]); //somewhere in the code
Community
  • 1
  • 1
Marko Hlebar
  • 1,973
  • 17
  • 18
  • Thanks. These were really great discussion. But my problem lies in using singleton in other scene. Using singleton is not helping me to share value between two scenes. I know I am wrong somewhere but don't know where. – Nitish Apr 14 '11 at 06:13
  • You can call your singleton object from anywhere in the program because it is static, like this [[MySingleton sharedInstance] myMethod]; sharedInstance is a class function, looks something like this +(MySingleton*) sharedInstance and it returns a ptr to your statically allocated object of class MySingleton*. myMethod is an instance method. The trick is that singleton lives until the termination of the program. You shoudn't release it unless you deem necessary. Also you don't have to alloc/init your singleton, since it is done only once in the sharedInstance call. CCDirector is a singleton too. – Marko Hlebar Apr 14 '11 at 06:25
  • And I wouldn't define my Scene as a singelton because scenes change rapidly. You would be stuck with all your scenes in the memory. Rather build a sharedData class where you store your data. And I think this is where your problem is [[CCDirector sharedDirector]replaceScene:[HelloWorld node]]; because calling node is actually returning a new autoreleased instance of HelloWorld scene. – Marko Hlebar Apr 14 '11 at 06:38
  • Thanks a lot KakoSquid :) I really worked. And your point regarding creation of separate Data class is really useful. – Nitish Apr 14 '11 at 07:26