I have a few question on memory management with cocos2d. So far my game I'm working on has 2 scenes an intro (displays a sprite and a label) and a menu scene that has a couple of sprites. I ran what I have so far in instruments and it has 8 leaks detected, and I'm not sure what I'm doing wrong.
The intro scene looks like this and after 6 seconds I schedule a method to get called to switch to the menu scene.
#import "Intro_Scene.h"
#import "Main_Menu.h"
#import "Label.h"
@implementation Intro_Scene
@synthesize logo,label;
-(id) init
{
self = [super init];
if(self != nil)
{
//Load logo image and set position
logo = [Sprite spriteWithFile:@"AVlogo_1.png"];
logo.position = ccp(-50, 0);
logo.scale = 1.8f;
[self addChild: logo];
//Creates 3 actions for the logo sprite
id action0 = [MoveTo actionWithDuration:0 position:ccp(160,270)];
id action1 = [FadeIn actionWithDuration:3];
id action2 = [FadeOut actionWithDuration:3];
//Logo runs the actions
[logo runAction: [Sequence actions:action0,action1, action2, nil]];
//Schedules the changeScene method to switch scenes to main menu within 6 seconds of loading.
[self schedule: @selector(changeScene) interval:6.0f];
//Creates a label and positions it, Alternative Visuals
label = [Label labelWithString:@"Alternative Visuals" fontName:@"Verdana" fontSize:22];
label.position = ccp(160, 120);
[self addChild:label];
}
return self;
}
//Method called after intro has run its actions, after 6 seconds it switches scenes.
-(void)changeScene
{
[self removeChild:logo cleanup:YES];
[self removeChild:label cleanup:YES];
Main_Menu *mainMenu = [Main_Menu node];
[[Director sharedDirector] replaceScene: mainMenu];
}
-(void)dealloc
{
[self unschedule:@selector(changeScene)];
[[TextureMgr sharedTextureMgr] removeUnusedTextures];
[label release];
[logo release];
[super dealloc];
}
@end
Have I released everything correctly? When the director switches scenes does the dealloc method get called? This part below, is this the proper way to do this? Under instruments it's detecting the leaks at +[CocosNode node].
Main_Menu *mainMenu = [Main_Menu node];
[[Director sharedDirector] replaceScene: mainMenu];
This is the implementation for the main menu...
#import "Main_Menu.h"
#import "Sprite.h"
#import "cocos2d.h"
@implementation Main_Menu
@synthesize background, controlLayer;
-(id) init
{
self = [super init];
if(self != nil)
{
//Create the default background for main menu not including directional pad and highlight box
background = [Sprite spriteWithFile:@"Main_Menu_bg.png"];
background.position = ccp(160,240);
[self addChild:background];
//Adds the control later class to the main menu, control layer class displays and controls the directional pad and selector.
ControlLayer *layer = [[ControlLayer alloc] init];
self.controlLayer = layer;
[layer release];
[self addChild: controlLayer];
}
return self;
}
-(void) dealloc
{
[self removeChild:background cleanup:YES];
[self removeChild:controlLayer cleanup:YES];
[[TextureMgr sharedTextureMgr] removeUnusedTextures];
[background release];
[controlLayer release];
[super dealloc];
}
@end
Once again am I doing everything correctly? And lastly the controlLayer looks like this...
#import "ControlLayer.h"
@implementation ControlLayer
@synthesize directPad, selector;
-(id) init
{
self = [super init];
if(self != nil)
{
//Creates the directional pad
directPad = [Sprite spriteWithFile:@"DirectionalPad.png"];
directPad.position = ccp(158,60);
[self addChild:directPad];
//Creates the box to demonstrate selection
selector = [Sprite spriteWithFile:@"Select.png"];
selector.position = ccp(157,362);
[self addChild:selector];
self.isTouchEnabled = YES;
}
return self;
}
//Called when screen has been touched then released
- (BOOL) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
//Detect down press
if(point.x >= 137 && point.x <= 183 && point.y <= 468 && point.y >=418)
{
[self down];
}
//Detect up press
if(point.x >= 137 && point.x <= 183 && point.y <= 418 && point.y >=368)
{
[self up];
}
return YES;
}
//Sends selector down
-(void) down
{
//Gets current position of selector
CGPoint currentPoint = [selector position];
int y = currentPoint.y;
int x = currentPoint.x;
//Selector speed animation, which will vary depending on location of selector
double selectorSpeed = .4;
//Sets new y position if selector not at the bottom
if(y <= 362)
{
y = y - 58;
}
//Set new y position once the selector reaches the bottom, then adjusts the speed of selector animation
if(y < 188)
{
y=362;
selectorSpeed = .2;
}
//Creates new point for selector then moves selector
CGPoint newPoint = CGPointMake(x,y);
id actionTo = [MoveTo actionWithDuration: selectorSpeed position:newPoint];
[selector runAction: actionTo];
}
//Sends selector up
-(void) up
{
//Gets current position of selector
CGPoint currentPoint = [selector position];
int y = currentPoint.y;
int x = currentPoint.x;
//Selector speed animation, which will vary depending on location of selector
double selectorSpeed = .4;
//Sets new y position if selector not at the top
if(y >= 188)
{
y = y + 58;
}
//Set new y position once the selector reaches the top, then adjusts the speed of selector animation
if(y > 362)
{
y=188;
selectorSpeed = .2;
}
//Creates new point for selector then moves selector
CGPoint newPoint = CGPointMake(x,y);
id actionTo = [MoveTo actionWithDuration: selectorSpeed position:newPoint];
[selector runAction: actionTo];
}
-(void) dealloc
{
[self removeChild:selector cleanup:YES];
[self removeChild:directPad cleanup:YES];
[[TextureMgr sharedTextureMgr] removeUnusedTextures];
[directPad release];
[selector release];
[super dealloc];
}
@end
I'm new to cocos2d and wanted to make sure before I get to deep into this project I'm doing everything correctly. Is their anything I'm doing wrong, if so please point it out so I can correct it.
I have been reading multiple tutorials, and been reading the forums for awhile... Also have been looking at a few example programs.
Thanks in advanced for any help!