Game Development Community

Ninja Platform, Why?

by Jumex · in Torque Game Builder · 07/30/2007 (10:06 pm) · 2 replies

The instructions have you set a true false value to left and right movment then has you later assign a direction and speed based on the True False.

function playerLeft()
{
$Pguy.moveLeft = true;
}

function playerLeftStop()
{
$Pguy.moveLeft = false;
}

function playerRight()
{
$Pguy.moveRight = true;
}

function PlayerRightStop()
{
$Pguy.moveRight = false;
}


Would it not be easier to simply do this and call it done?

function playerleft()
{
$Pguy.setLinearVelocityX(-60);
}

function playerleftstop()
{
$Pguy.setLinearVelocityX(0);
}

function playerright()
{
$Pguy.setLinearVelocityX(60);
}

function playerrightstop()
{
$Pguy.setLinearVelocityX(0);
}

#1
08/05/2007 (10:55 am)
You can do it however you want to... I could write it 3 different ways.. there is not right/wrong. Sounds good!
#2
08/05/2007 (11:16 am)
There are a couple of advantages of decoupling the input from the actual movement implementation in the way the example gives:

--it makes state based animation changing easier. All you need to do is to check the boolean for "moveLeft", to see if you should be playing a "moving left" animation.

--it allows you to more easily handle keys pressed in one order, then released in a different order. Consider the following set of user input, where "a" is move left, "d" is move right:

a d (release d) (release a).

To the player, they would think they should start moving left, then start moving right, then after the d key is released, start moving left again, but in the second approach the original poster lists, what would actually happen here is that the player object would start moving left, then start moving right, then stop moving at all when the d key is released, even though the a key is still pressed.

--finally, it allows for things like one way doors, only allowing collisions if moving in a certain direction, and other direction based game mechanics somewhat easier to handle.