Sorry for the newbie question but I was wondering what the difference is between these two different setups for the scene & the layer? I have tried both ways and each one works but I just don't know what the difference is or which one I should use.
#implementation Game
+(id) scene {
CCScene *scene = [CCScene node];
[scene addChild:[Game node]];
return scene; }
Or this way.
#implementation Game
+(id) scene {
CCScene *scene = [CCScene node];
Game *layer = [Game node];
[scene addChild:layer];
return scene; }
I don't see any difference between your 2 sample of code. Your 2 methods are identically the same.
[Game node] returns a layer so in the first case you add it directly into your scene and in the 2 example you just put it into a variable then add it into your scene.
For the compiler this is the same thing here.
Related
To be able to pan and zoom some of the content in screen I decided to use CCLayerPanZoom extension. When I look at the source code I can see that it derives from the CCLayer class. So I change the parent class of the node that's pushed to the navigation stack from CCLayer to CCLayerPanZoom. But what I get when the app launches is just a black screen. To make it cleaner, I created a new class, derived it from CCLayerPanZoom, added a test sprite onto it in the init method and pushed it to the navigation stack in the AppDelegate.m. Still I got nothing, just a black screen. Here're the two methods that I've implemented in my class:
#interface TestPanZoom : CCLayerPanZoom {
}
+(CCScene *) scene;
#end
#implementation TestPanZoom
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
TestPanZoom *layer = [TestPanZoom node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id)init{
if(self=[super init])
{
CCSprite *sprite=[CCSprite spriteWithFile:#"Default.png"];
sprite.scale=0.5;
[self addChild:sprite];
}
return self;
}
#end
I've got an end of level layer added to game, each level has its own scene. I want to be able to restart the current scene. Obviously the scene will change but the layer will remain the same. How is this done. I've tried-
CCScene *currentScene = [[CCDirector sharedDirector]runningScene];
[[CCDirector sharedDirector]replaceScene:currentScene];
Thanks
This does not work because you can't replace the same scene object with itself:
CCScene *currentScene = [[CCDirector sharedDirector]runningScene];
[[CCDirector sharedDirector]replaceScene:currentScene];
Instead you have to create a new instance of your scene, like so:
[[CCDirector sharedDirector] replaceScene:[YourSceneClass scene]];
If you don't know what the current scene class is, then this ought to work:
CCScene *currentScene = [CCDirector sharedDirector].runningScene;
CCScene *newScene = [[[currentScene class] alloc] init];
[[CCDirector sharedDirector] replaceScene:newScene];
Assuming you're using ARC as everyone should these days. Otherwise add an autorelease.
I ran into the same problem. I tried this
CCScene *currentScene = [CCDirector sharedDirector].runningScene;
CCScene *newScene = [[[currentScene class] alloc] init];
[[CCDirector sharedDirector] replaceScene:newScene];
and it gave me a blank screen.
The problem is this line
CCScene *newScene = [[[currentScene class] alloc] init];
[currentScene class] actually returns CCScene..
Hence
[CCScene alloc] init] gives us a blank screen.
The way how I got around this problem was by setting tag for each of my scene class.
For example:
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
scene.tag = 1;
// 'layer' is an autorelease object.
GameOneLayer * layer = [[[GameOneLayer alloc] init];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Hope this helps.
I'm trying to implement a game with cocos2d. I enabled arc according to the instructions on this tutorial.
http://www.learn-cocos2d.com/2012/04/enabling-arc-cocos2d-project-howto-stepbystep-tutorialguide/
I realized a weird behavior after replacing game scene with main menu scene and I can't figure out the problem. After replacement, the new scene appears and works as I expected, but the game scene (old scene) still reacts touches. I thought that when I replace the scene, old scene should be removed completely, but it continue to live under the new scene.
Some of the relevant code is as follows:
Singleton:
+(void) go: (CCLayer *) layer{
CCDirector *director = [CCDirector sharedDirector];
CCScene *newScene = [Singelton wrap:layer];
if ([director runningScene]) {
[director replaceScene: [CCTransitionFlipX transitionWithDuration:0.5 scene:newScene]];
} else {
[director pushScene:newScene];
}
}
+(CCScene *) wrap: (CCLayer *) layer{
CCScene *newScene = [CCScene node];
[newScene addChild: layer];
return newScene;
}
+(void) mainMenu
{
CCLayer *layer = [MainMenu node];
[Singleton go:layer];
}
When I need to go to main menu scene I call singleton class as follows
[Singleton mainMenu]
How can I kill the game scene after menu scene appears?
Thanks for your help.
What does Singleton do? I suspect that it might be the cause, holding on to references of the Scene/Layer or any other nodes while/after replacing a scene.
I am trying to construct a number of scenes in my GameManager singleton init.
The scene is created via
- (id)init
{
self = [super init];
if (self) { // 'mainScene' is an autorelease object.
mainScene = [CCScene node];
...
}
GameManger holds a strong reference to mainScene:
#interface GameManager : NSObject
{
CCScene* mainScene;
}
But if I try to push the scene later with
[[CCDirector sharedDirector] pushScene:mainScene];
I get EXC_BAD_ACCESS
If I create and immediately push then everything works. Shouldn't the default __strong reference keep the object allocated?
Thanks in advance for any help....
Figured it out ... [CCScene node] is a convenience factory method that does:
[[[self alloc] init] autorelease];
but since I am using arc...I dont want that - I want
mainScene = [[CCScene alloc]init];
instead of
mainScene = [CCScene node];
Here's the code I know to change scenes with cocos2d:
[[CCDirector sharedDirector] replaceScene:[HelloWorld scene]];
But I wonder if it's possible I can switch the scenes with some parameters.
I tried this method:
HelloWorld *scene = [HelloWorld scene];
[scene initWithInput:0];
[[CCDirector sharedDirector] replaceScene:scene];
-(void)initWithInput:(int)input is what I wrote for test in HelloWorld class.
And it does't work, does any one know how to do it?
Try overriding the scene method. Something like
+(id) sceneWithInput:(int) i
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld nodeWithInput:i];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Then you would call
[[CCDirector sharedDirector] replaceScene:[HelloWorld sceneWithInput:0]];