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.
Related
I enabled ARC for my cocos2d project.
Now i try doing the following:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
[self addChild:menu];
And in the BuildTowerMenu class:
+(id)menuAtLocation:(CGPoint)location {
return [[self alloc] initMenuAt:location];
}
-(id) initMenuAt:(CGPoint)location {
if (self = [super init]) {
self.position = location;
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[self addChild:item1];
}
return self;
}
But for some reason, the Sprite never shows up. After a bit of debugging i see that when i return from menuAtLocation, the CCSprite is still in the Array of children of BuildTowerMenu, but empty (only got an id).
If i actually add the sprite from outside it works and the Sprite is displayed:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[menu addChild:item1];
[self addChild:menu];
Any hints on what i did wrong here?
P.S.: i added a breakpoint in the dealloc of CCSprite, which never gets called (i guess it should be called if ARC is releasing it)...
I think that the problem is that you set equal positions to your menu and sprite.
I mean that this part of code
CGPoint location = ccp(200.f, 200.f);
[menu setPosition: location];
[sprite setPosition: location];
[menu addChild: sprite];
will add your sprite with position position (400.f, 400.f), relatieve to the menu's parent. You are doing almost the same thing in your BuildTowerMenu's initMenuAt: method/
As already commented on the initial question_:
Ok, it had nothing to do with ARC; the Sprite which seemed to be released was there, it was just a debugger - bug which didnt display it correctly. The problem was actually the line self.position = location; After moving the setposition after the addChild, everything worked.
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 am having some problem with removing my spritesheets after my scene exit..
I basically follow Ray's instructions by removing unused textures in init
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCDirector sharedDirector] purgeCachedData];
and in dealloc I have
CCTexture2D * texture = spriteNode.textureAtlas.texture;
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromTexture:texture];
[[CCTextureCache sharedTextureCache] removeTexture:texture];
Reference
This works fine if the transition to scene is not the current scene.
But when I tried to "restart" the current scene it crashes.
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[currentScene scene] withColor:ccBLACK]];
Seems like the problem is that when replacing the current scene by the "new" current scene.. the "new" current scene init is called before the current scene is deallocated. Hence my "new" current scene spritesheet got deallocated right after it's being created.
How can I properly remove the spritesheets in this case?
Or is my approach to restart the current scene incorrect?
Update:
I was trying to add a loading scene as advised but couldn't make it work.. here's my loading scene
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
LoadingLayer * layer = [[[LoadingLayer alloc]init]autorelease];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init{
if(self = [super init]){
winSize = [CCDirector sharedDirector].winSize;
CCLabelTTF * loadingLabel = [CCLabelTTF labelWithString:#"Loading..." fontName:#"Marker Felt" fontSize:30];
}
loadingLabel.position = ccp(winSize.width/2, winSize.height/2);
[loadingLayer addChild:loadingLabel];
[self scheduleUpdate];
}
return self;
}
-(void) update:(ccTime)dt{
[self unscheduleUpdate];
NSString * bgPlist = #"backgroundsIPAD.plist";;
NSString * hudPlist = #"hudSpritesIPAD.plist"
NSString * gameOnePlist = #"gameOneSpritesIPAD.plist";
// load background
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:bgPlist];
// load hud
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:hudPlist];
// load sprites
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:gameOnePlist];
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:2.0 scene:[GameTwoLayer scene] withColor:ccBLACK]];
}
This will give me a splash of the GameTwoLayer in this case, then a black screen..
What am I doing wrong?
Indeed, the new scene's init runs before your current scene is deallocated. Makes sense, since you're creating a new scene object within your current scene object.
You should not remove textures etc during init for that reason. It's just not the right place to do so. The scene that has been using the resources should be responsible for removing those resources. Removing textures etc should be done in the dealloc method of the scene.
If you need to avoid the memory spike with both scenes in memory at the same time, you'll have to add an in-between "loading" scene so that the previous scene will be deallocated before the next scene starts.
I am developing a simple game app.
I am required to have two screens one on left & other on right, just like on scroll view with pagination having 2 screens to toggle between. I am detecting swipes with the help of RootViewController class and successfully swiping left and right screens.
But the problem is I have to have infinitely running rotateBy action on both screens each running this action on single sprite placed on center of screen.
I am using a main scene called SelectWorld & two are it's sub scenes.
First screen is called factory & other is called stack.
Following is my code:
Select World Screen:
#implementation SelectWorld
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
SelectWorld* layer = [SelectWorld node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
[RootViewController singleton].swipeCallBackHandler = self;
[[RootViewController singleton] enableSwipeDetection];
factory = [[ColorfulFactory scene] retain];
stack = [[CoinsStack scene] retain];
[self addChild:factory];
isOnFactory = YES;
}
return self;
}
-(void)swipedLeft
{
if(isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = NO;
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:stack];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
-(void)swipedRight
{
if(!isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = YES;
CCTransitionScene* transitionalScene = [CCTransitionSlideInL transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
Here's the code for factory only, same goes for stack-
#implementation ColorfulFactory
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
ColorfulFactory* layer = [ColorfulFactory node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
CGSize size = [CCDirector sharedDirector].winSize;
CCLabelTTF* info = [CCLabelTTF labelWithString:#"Colorful Factory" fontName:#"Helvetica" fontSize:35.0];
info.position = ccp(size.width/2, size.height-50);
[self addChild:info];
CCSprite* sprite = [CCSprite spriteWithFile:#"Icon.png"];
sprite.position = ccp(size.width/2, size.height/2);
[self addChild:sprite];
sprite.tag = 123;
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:60 angle:360.0];
CCRepeatForever* repeatForever = [CCRepeatForever actionWithAction:rotateBy];
[sprite runAction:repeatForever];
repeatForever.tag = 456;
}
return self;
}
Now the problem is, in first two swipes actions are running fine, but as soon as I try to swipe more than two or three times, actions got stopped. I haven't written a single line to stop this in both the classes. I require it there running forever.
Your mistake is that you're re-using the same scene object for every scene change. You can't do that in cocos2d, you have to create a new instance of the scene object.
You should never retain a scene object, since there ought to be only one active scene at any one time (with the exception of the duration of a scene transitioning to another scene - but that's handled by cocos2d internally).
For example:
CCScene* factory = [ColorfulFactory scene];
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
If you need both objects in memory, make them layers (or nodes), not scenes. Don't use replaceScene but instead animate the layers with regular moveTo actions in and out of the screen.
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.