Game Development Community

Q about Basic Tutorial

by Jason McIntosh · in Torque Game Builder · 02/27/2005 (10:33 am) · 3 replies

In the basic tutorial, it mentions this function:

/-----------------------------------------------------------------------------
// Player Up (Stop).
//-----------------------------------------------------------------------------
function playerUpStop()
{
	// If we're moving up then nullify any upward movement.
	if ( $player.getLinearVelocityY() < 0 )
		$player.setLinearVelocityY( 0 );	
}

...and how it avoids some "subtle bugs" with the if() statement. I'm just curious what subtle bugs this refers to. Trying to get a deeper understanding of the engine than what's in the docs ATM.

Thanks!

#1
02/27/2005 (11:17 am)
Perhaps, and I'm guessing here, it has to do with the order in which the events occur. Imagine you are holding down the Up key and then release at the exact same moment you hit Down. If the events fire in this order (and you didn't have the ifs):

Up released (linear velocity is now 0)
Down pressed (linear velocity is now 10)

then everything would be fine. Reverse them:

Down pressed (linear velocity is now 10)
Up released (linear velocity is now 0) ** Whoops! **

This isn't a bug in the engine. They are just making an elegant solution to the problem above with the ifs.
#2
02/27/2005 (11:26 am)
Try it, you can definitely see what happens if you don't put in the if statement. Attempt to rapidly switch between up and down or right and left and you will be unable to move while the key is held.
#3
02/27/2005 (12:17 pm)
Yep, you guys have got it spot on.

- Melv.