Can't get play button to properly load first level.
by Dean Edwards · in iTorque 2D · 06/14/2012 (9:53 am) · 14 replies
Hi,
I have created a behavior called LevelChangeBehavior and applied it the Play button within my Menu scene. All I want it to do is load the first level scene which is called emptyLevel.t2d (I didn't change the name when I created it) but the game crashes whenever I press the button and the level does not load.
Here is the code for my LevelChangeBehavior:
if (!isObject(LevelChangeBehavior))
{
%template = new BehaviorTemplate(LevelChangerBehavior);
%template.friendlyName = "Change Level Button";
%template.behaviorType = "Button";
%template.description = "Changes to Level when the image is clicked.";
}
function LevelChangeBehavior::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
//allows object to be clicked
}
function LevelChangeBehavior::onTouchDown(%this, %worldPos)
{
sceneWindow2D.loadLevel("/data/levels/emptyLevel.t2d");
}
Thank You in advance for answering.
I have created a behavior called LevelChangeBehavior and applied it the Play button within my Menu scene. All I want it to do is load the first level scene which is called emptyLevel.t2d (I didn't change the name when I created it) but the game crashes whenever I press the button and the level does not load.
Here is the code for my LevelChangeBehavior:
if (!isObject(LevelChangeBehavior))
{
%template = new BehaviorTemplate(LevelChangerBehavior);
%template.friendlyName = "Change Level Button";
%template.behaviorType = "Button";
%template.description = "Changes to Level when the image is clicked.";
}
function LevelChangeBehavior::onBehaviorAdd(%this)
{
%this.owner.setUseMouseEvents(true);
//allows object to be clicked
}
function LevelChangeBehavior::onTouchDown(%this, %worldPos)
{
sceneWindow2D.loadLevel("/data/levels/emptyLevel.t2d");
}
Thank You in advance for answering.
#2
06/15/2012 (6:05 am)
Is it the misspelled template name "LevelChange*r*Behavior" or is that a typo in the message?
#3
06/15/2012 (6:53 am)
I changed that and the button still doesn't work.
#4
sceneWindow2D.loadLevel("data/levels/emptyLevel.t2d");
Your onTouchDown has incorrect parameters. Change it to this:
onTouchDown(%this, %touchID, %worldPos)
Don't think it's causing this problem though since you're not using the parameters.
06/15/2012 (7:35 am)
Try removing the initial slash on your loadLevel call so it's like this:sceneWindow2D.loadLevel("data/levels/emptyLevel.t2d");
Your onTouchDown has incorrect parameters. Change it to this:
onTouchDown(%this, %touchID, %worldPos)
Don't think it's causing this problem though since you're not using the parameters.
#5
06/15/2012 (7:41 am)
As a best practices note, I would suggest moving your actual level loading code to a method on its own so that it is reusable if you need to load the level by other means or add more stuff around it. In your onTouchDown method only put a call to the new method that does the level load.
#6
Thanks for advice, I will look into that. Still no idea why this isn't working though.
06/15/2012 (8:07 am)
Still no luck. Its not crashing but nothing happens when I click the button.Thanks for advice, I will look into that. Still no idea why this isn't working though.
#7
Also - is emptyLevel.t2d empty? If so you'll be staring at what will look like the screen you were just at but your mouse will leave pointer trails all over.
You can also use the echo() function in your onTouchDown() method to drop messages to yourself in your console.log file to aid debugging.
06/15/2012 (12:49 pm)
Drop the leading / from the file string.function LevelChangeBehavior::onTouchDown(%this, %worldPos)
{
sceneWindow2D.loadLevel("data/levels/emptyLevel.t2d");
}Also - is emptyLevel.t2d empty? If so you'll be staring at what will look like the screen you were just at but your mouse will leave pointer trails all over.
You can also use the echo() function in your onTouchDown() method to drop messages to yourself in your console.log file to aid debugging.
#8
06/15/2012 (1:14 pm)
I have deleted the slash and it didn't change anything. emptyLevel.t2d is not empty and the button does not load any scene at all.
#9
If so, try scheduling the level load. This is how I've done it before.
Create a new method in your game.cs:
Then change your onTouchDown:
06/15/2012 (3:38 pm)
You have a loadLevel line the loads your menu scene. What happens if you replace that loadlevel line with the one from your onTouchDown? Does your level load successfully?If so, try scheduling the level load. This is how I've done it before.
Create a new method in your game.cs:
function loadGameLevel()
{
echo("loadGameLevel start");
sceneWindow2D.loadLevel("data/levels/emptyLevel.t2d");
echo("loadGameLevel end");
}Then change your onTouchDown:
function LevelChangeBehavior::onTouchDown(%this, %touchID, %worldPos)
{
echo("LevelChangeBehavior::onTouchDown start");
schedule(100, 0, loadGameLevel);
echo("LevelChangeBehavior::onTouchDown end");
}
#10
06/15/2012 (5:13 pm)
Noby is correct. Always schedule level loads or else your app might crash.
#11
06/16/2012 (3:50 am)
OMG, it worked! Thank you so much!! Thanks for the advice. :D
#12
06/16/2012 (4:50 pm)
Great to hear! Good luck with further progress.
#13
08/12/2012 (1:11 am)
I'm new to iTorque. Still working through the tutorials. When I load a level, like when one level is complete and I load a new one, do I need to erase the old one? Or do I just load the new level?
#14
I wonder if there's something else going on there that I just haven't hit. Do you know any specifics Johnny?
08/13/2012 (8:26 am)
That's really interesting about scheduling load levels. I've not done it that way yet in my app and I load and unload levels all the time.I wonder if there's something else going on there that I just haven't hit. Do you know any specifics Johnny?
Torque Owner Dean Edwards
Default Studio Name