i want know if there is a way to push a scene in cocos2d 2.0 and pass some parameter to this pushed scene, for example, i know that to push a scene i use this:
[[CCDirector sharedDirector] pushScene:[HelloWorldLayer scene]];
and this push the helloworldlayer, that is a simple layer:
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
but i want pass to this layer some parameter, so when the layer is pushed i can use the parameter i passed.
how i can do it?
you can do something like +(CCScene *) sceneWithParameter:(ParameterType)parameter;
instead of +(CCScene *) scene;
First you will have to create a method to call with the parameter like so
HelloWorldLayer.h
@interface HelloWorldLayer : CCLayer
{
}
+(CCScene *)sceneWithParam:(id)parameter;
@end
HelloWorldLayer.m
@implementation HelloWorldLayer
+(CCScene *)sceneWithParam:(id)parameter
{
[[parameter retain]doSomething];
CCScene * scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if(self = [super init])
{
}
return [super init];
}
// All your methods goes here as usual
@end
Then you push it by calling
[[CCDirector sharedDirector] pushScene:[HelloWorldLayer sceneWithParam:obj]];
Now this might still not be enough, if you need the parameter inside your layer you will need to do the same thing for the layer. Create the initmethod with the method and then pass it further to the layer in the sceneWithParam: method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With