Game Development Community

Cant end, load new level ( some difficulties)

by Vlad I · in Torque Game Builder · 05/13/2010 (1:43 am) · 4 replies

I'm trying to end a level and load a new one, I know how to do it using the console though, but cannot make it work in my .cs
Basically I have two levels level01 and level02, I want to end level01 and start level02 when Box1 and Box2 together removed from the game. I made it work with only one Box but that's all
Here is the code of my Boxes.cs :

function Boxes::onAdd(%this)
{
%this.setUseMouseEvents( true ); // enabling mouse event
}

function Box01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box01))
%this.safedelete();
}

function Box02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box02))
%this.safedelete();
}

function nextLevel::onLevelLoaded( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box01);isObject(Box02)) // console gives me an error here

SceneWindow2D.endLevel();
sceneWindow2D.loadLevel("game/data/levels/level02.t2d");

}
I've searched the forums,tutorials and only found how to end a level on a collision.

#1
05/13/2010 (1:27 pm)
There's a couple of problems.

First, your if-statement in the "onLevelLoaded" has improper syntax. You'd probably want something more like "if( isObject(Box01) && isObject(Box02) ) return;".

More importantly, I don't think the "onLevelLoaded" is working like you're expecting it to. You probably want something more like:

$totalNumberOfBoxes = 0;

// I'm assuming that each box has the class name "Boxes".
function Boxes::onAdd(%this)
{
  %this.setUseMouseEvents( true ); // enabling mouse event
  $totalNumberOfBoxes++;
}

function Boxes::onMouseDown( %this, %modifier, %worldPosition, %clicks) 
{
  %this.safeDelete();
  $totalNumberOfBoxes--;
  if( $totalNumberOfBoxes == 0 )
    schedule( 0, 0, loadNextLevel );
} 

function loadNextLevel()
{
  sceneWindow2D.endLevel();
  sceneWindow2D.loadLevel("game/data/levels/level02.t2d");
}

Edit: I made a slight change to the code to make it more stable.
#2
05/15/2010 (4:26 am)
Hi William, long time no see!
Thank you so much for helping me on this one. It worked perfect with no errors.
However I forgot to mention that I have Star objects (star01, star02) which get deleted too with the Boxes (Box01, Box02) I just thought it wasnt that important to mention about
So the code was looking like that :

function Boxes::onAdd(%this)
{
%this.setUseMouseEvents( true ); // enabling mouse event
}
function Box01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box01))
%this.safedelete();
Star01.safedelete(); // The line I didnt include in the first post
}
function Box02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box02))
%this.safedelete();
Star02.safedelete(); // The line I didnt include in the first post
}
etc.
_____________________________________________________________________
So my new code is :

$totalNumberOfBoxes = 0;
function Box01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box01))
%this.safedelete();
Star01.safedelete(); // The line I didnt include in the first post
}
function Box02::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box02))
%this.safedelete();
Star02.safedelete(); // The line I didnt include in the first post
}
function Box03::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
if(isObject(Box03))
%this.safedelete();
Star03.safedelete(); // The line I didnt include in the first post
}
// I'm assuming that each box has the class name "Boxes".
function Boxes::onAdd(%this)
{
%this.setUseMouseEvents( true ); // enabling mouse event
$totalNumberOfBoxes++;
}

function Boxes::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
%this.safeDelete();
$totalNumberOfBoxes--;
if( $totalNumberOfBoxes == 0 )
schedule( 0, 0, loadNextLevel );
}

function loadNextLevel()
{
sceneWindow2D.endLevel();
sceneWindow2D.loadLevel("game/data/levels/level02.t2d");
}

It shows no errors though, yet doesnt end/load next level,still trying to figure it out.
#3
05/15/2010 (9:41 am)
Once again, assuming that you've given each box the class name "Boxes", you'll ened to change your main functions to something like:

function Box01::onMouseDown( %this, %modifier, %worldPosition, %clicks)
{
  Star01.safedelete();
  Parent::onMouseDown( %this, %modifier, %worldPosition, %clicks );
}

What would be even better is if you tied the star into the box through a dynamic variable:
%star = new 2dStaticSprite() {...};
%box = new t2dStaticSprite() {class = "Boxes"; ...};
%box.myStar = %star;

Then all of your code could look like
$totalNumberOfBoxes = 0; 

function Boxes::onAdd(%this) 
{ 
  %this.setUseMouseEvents( true ); // enabling mouse event 
  $totalNumberOfBoxes++; 
} 

function Boxes::onMouseDown( %this, %modifier, %worldPosition, %clicks) 
{ 
  %this.myStar.safeDelete();
  %this.safeDelete();
  $totalNumberOfBoxes--; 
  if( $totalNumberOfBoxes == 0 ) 
    schedule( 0, 0, loadNextLevel ); 
} 

function loadNextLevel() 
{ 
  sceneWindow2D.endLevel(); 
  sceneWindow2D.loadLevel( "game/data/levels/level02.t2d" ); 
}
#4
05/15/2010 (8:49 pm)
Thank you William. Your assumption is correct, it's like you read my mind! :)
I used the first method and it works perfect with no errors, I tried to implement the others with out success, but the first one does the job right.
Thanks again.