Game Development Community

OnWorldLimit - multiple callback problem

by Philip Mansfield · in Torque Game Builder · 09/21/2005 (4:09 am) · 2 replies

This is a bit of a repost from another thread, but I figured it might get a better response here. Anyway, here's the problem:

I have a player sprite which is a 4 frame animation that loops. When it hits the side or top of the screen I want it to stop. When it hits the bottom, it should explode. Sounds easy enough, and I setup the world limit and made it Clamp to stop the player moving. That deals with 3 of the sides. The bottom I figured I should be able to deal with through the onWorldLimit callback, but it's not working as expected.

When the player hits the side of the screen, the callback fires once. When he hits the top or bottom of the screen the callback fires multiple times. I can't see any reason why this would be. I've tried it with a clean install of T2D and a freshly compiled version of the engine (the only changes I've made are to encrypt the game assets). The version of the .exe from the example directory also gives the same problem. So maybe it's a problem with my scripting rather than the engine, but a little help would be much appreciated.

Here's the code:

Player Init:
function initPlayer()
{
	// define player
	$player = new fxAnimatedSprite2D() { scenegraph = t2dSceneGraph; };
	$player.setPosition("0 0");
	$player.setSize( "10 5" );
	$player.playAnimation( helicopterAnimation );
	$player.setGroup(1);
	$player.setLayer(15);
	$player.setCollisionPolyPrimitive(4);
	$player.setCollisionScale("1 0.62");
	$player.setCollisionActive( true, false );
	$player.setCollisionMasks( BIT(3)|BIT(2), BIT(5)|BIT(10) );
	$player.setCollisionCallback( true );
	$player.setWorldLimit( CLAMP, "-48 -36 48 28", true );
	$player.tag = "player";
	$player.speed = 10;
}
Movement Code:
function playerDown()
{
	// Set the player moving down.
	$player.setLinearVelocityY( $player.speed );
}

function playerDownStop()
{
	// If we're moving up then nullify any downward movement.
	if ( $player.getLinearVelocityY() > 0 )
		$player.setLinearVelocityY( 0 );
}

function playerLeft()
{
	// Set the player moving left.
	$player.setLinearVelocityX( -($player.speed) );
}

function playerLeftStop()
{
	// If we're moving up then nullify any leftward movement.
	if ( $player.getLinearVelocityX() < 0 )
		$player.setLinearVelocityX( 0 );
}
Callback:
function fxSceneObject2D::onWorldLimit(%this, %limitMode, %wall)
{
	if (%this.tag $= "player")
	{
		if (%wall $= "bottom")
		{
			echo("crash");
		} else
		{
			echo ("hit wall");
		}
	}
}
Console log (initially I moved all the way to the left and kept the key held down for a second, then I moved away from the left wall, and down to the bottom of the screen where I also kept the key held down. There were a lot more 'crash' entries in the console.log file):
hit wall
crash
crash
crash
crash
crash
crash
crash
crash
crash
crash
crash
crash
I think that's about all the relevant code, but I can obviously post more if needed. This is driving me a little insane :(

#1
09/21/2005 (9:01 am)
OK, I've made some progress. If I comment out:
$player.setCollisionScale("1 0.62");
From the player init section, then I get one callback when I hit the top or bottom. Putting the line back in gives me multiple callbacks from the top and bottom.

I haven't tested it, but I suspect if I scaled the collision poly in the X direction, I would get multiple callbacks from the sides of the world limit as well.

Next step is to define a custom collision poly and see if I get the same problem with that...
#2
09/21/2005 (9:27 am)
Bizzarely:
$player.setCollisionPolyCustom(7, "-0.9063 -0.3594 -0.9766 0.3125 -0.7891 0.5781 -0.2344 0.5781 0.9375 0.0938 0.9844 -0.2656 -0.0469 -0.6250");
Give me single hits on the top and bottom but multiple hits on the side walls.

This is making my head hurt, but single callbacks on the bottom are what I want, so I'm done with it for now.