Game Development Community

Proper way to load a level and unload the previous one

by Andrea Farid Marsili · in iTorque 2D · 10/31/2011 (8:14 am) · 3 replies

Hi. I've go a few memory problem On my iPod4 after loading one or two level the device crash. This not happen on my iPad2. I think that my problem is about memory management.
Can someone tell me what's the proper way to unload a level and load a new one?

I load two level in one time because one is for my GUI. Here my load level behavior

if(!isObject(MainMenuButtonBehavior)){
	%template = new BehaviorTemplate(MainMenuButtonBehavior);

	%template.friendlyName = "Level Button";
	%template.behaviorType = "LevelManagement";
	%template.description  = "Loads a new level when clicked";

	%template.addBehaviorField(level, "The level to load on mouse down", string, "");
	%template.addBehaviorField(guiLevel, "The GUI level to load on mouse down", string, "");
	%template.addBehaviorField(loadIcon, "Want to call the loading icon?", bool, false);
}

function MainMenuButtonBehavior::onBehaviorAdd(%this){
	%this.owner.setUseMouseEvents(true);
}

function MainMenuButtonBehavior::onTouchDown(%this, %touchID, %worldPos){
	if(%this.loadIcon){
		startLoadIcon();
	}

	%guiLevel = "~/../data/levels/" @ %this.guiLevel @ ".t2d";  
    hudWindow.schedule(500, "loadLevel", %guiLevel);

	%level = "~/../data/levels/" @ %this.Level @ ".t2d";  
    sceneWindow2D.schedule(500, "loadLevel", %level);
    
    $currentLevelName = %this.Level;
    
    echo("Level Name: " @ $currentLevelName);
}

I think that with:
hudWindow.endLevel(); 
sceneWindow2D.endLevel();

Inside "function MainMenuButtonBehavior::onTouchDown(%this, %touchID, %worldPos)" the problem should be resolved but I'm not sure of it.

Do you think that this tutorial is still good?

http://tdn.garagegames.com/wiki/Torque_2D/GenreTutorials/PlatformerLevels

#1
11/01/2011 (6:50 am)
@Andrea - Try putting more time between the schedules. Set the loadLevel for the HUD to about 600 or 700.
#2
11/01/2011 (9:04 am)
Here is my behavior code. Tried to do what you suggested to me but with no luck:

if(!isObject(MainMenuButtonBehavior)){
	%template = new BehaviorTemplate(MainMenuButtonBehavior);

	%template.friendlyName = "Level Button";
	%template.behaviorType = "LevelManagement";
	%template.description  = "Loads a new level when clicked";

	%template.addBehaviorField(level, "The level to load on mouse down", string, "");
	%template.addBehaviorField(guiLevel, "The level to load on mouse down", string, "");
	%template.addBehaviorField(loadIcon, "Want to call the loading icon?", bool, false);
}

function MainMenuButtonBehavior::onBehaviorAdd(%this){
	%this.owner.setUseMouseEvents(true);
}

function MainMenuButtonBehavior::onTouchDown(%this, %touchID, %worldPos){
	if(%this.loadIcon){
		startLoadIcon();
	}
	
	//--------------------------
	hudWindow.endLevel();   
	sceneWindow2D.endLevel();
	//--------------------------
	
	%guiLevel = "~/../data/levels/" @ %this.guiLevel @ ".t2d";  
    hudWindow.schedule(800, "loadLevel", %guiLevel); //500

	%level = "~/../data/levels/" @ %this.Level @ ".t2d";  
    sceneWindow2D.schedule(800, "loadLevel", %level); //500
    
    $currentLevelName = %this.Level;
    
    echo("Level Name: " @ $currentLevelName);
}
#3
11/04/2011 (11:32 am)
Any advice for me?