TGB - 4 Direction Movement
by Brian Gallops · in Game Design and Creative Issues · 04/13/2009 (11:33 pm) · 6 replies
Hello,
I am trying to get a character to move in only four directions on TGB. Right now he's doing eight and I only have animations for four directions. Also, is there a good way to have a key be overriden when another is pressed? For instance, I am moving up and then, while still holding up, I press right. Is there a way for the character to just move right and the up is ignored altogether? I ask this because I currently have movement set up so that if a key is released, the player stops. This means if I hold one key, then another and release the first key while still holding the second key down, my character stops.
All in all, I'm just trying to clean up my movement script. Any help would be much appreciated!
I am trying to get a character to move in only four directions on TGB. Right now he's doing eight and I only have animations for four directions. Also, is there a good way to have a key be overriden when another is pressed? For instance, I am moving up and then, while still holding up, I press right. Is there a way for the character to just move right and the up is ignored altogether? I ask this because I currently have movement set up so that if a key is released, the player stops. This means if I hold one key, then another and release the first key while still holding the second key down, my character stops.
All in all, I'm just trying to clean up my movement script. Any help would be much appreciated!
About the author
#2
06/02/2009 (11:54 am)
I checked out some of the behavior tutorials but didn't really find what I was looking for. Perhaps I overlooked it, but I continued on with scripting movement and got it to work in only 4 directions. The problem is that the animations are messed up. I'm going to keep looking into it and tweaking the code, but if there's something I'm overlooking in behaviors, please let me know.
#3
06/14/2009 (8:45 pm)
Are your animations coded into your keyboard bindings, or to the character's movements? Creating a simple check loop might help clear up the issue if you are coding the changes into the keybindings.
#4
function PlayerUp()
{
$MainCharacter.moveUp = true;
$MainCharacter.updateMovement();
}
//then skip the other directions...
function PlayerUpStop()
{
$MainCharacter.moveUp = false;
$MainCharacter.updateMovement();
$MainCharacter.playAnimation( mc_back_still_Animation);
}
//again, skipping the other directions to save space
function MainCharacter::updateMovement(%this)
{
if(%this.moveLeft)
{
$MainCharacter.setLinearVelocityX( -$MainCharacter.hSpeed );
$MainCharacter.playAnimation( mc_left_Animation );
}
So I showed one of each direction of code. Meaning, I showed code for moving and stopping code while facing upward, but I have code for down, left, and right, which looks the same. Also, to have only 4 directions, I put this in:
function MainCharacter::updateMovement(%this)
{
// this is where I had the if(%this.moveLeft) and set the velocity
// as well as the animations for the various movements.
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}
So that's basically what I've been using, but it feels (and probably looks) really cluttered, as though I'm making it harder than it needs to be. Like I stated earlier, if you need the entire file of code, I'd be happy to put it up here, just let me know.
06/15/2009 (9:40 am)
Yes, the animations are coded into the keyboard bindings. The way I have it written out seems to me to be the "long" way of doing it, as there is a long list of code and it's still not complete due to animation problems. I can show you what I've done for the character if that would help, but here's a little snippet of the code:function PlayerUp()
{
$MainCharacter.moveUp = true;
$MainCharacter.updateMovement();
}
//then skip the other directions...
function PlayerUpStop()
{
$MainCharacter.moveUp = false;
$MainCharacter.updateMovement();
$MainCharacter.playAnimation( mc_back_still_Animation);
}
//again, skipping the other directions to save space
function MainCharacter::updateMovement(%this)
{
if(%this.moveLeft)
{
$MainCharacter.setLinearVelocityX( -$MainCharacter.hSpeed );
$MainCharacter.playAnimation( mc_left_Animation );
}
So I showed one of each direction of code. Meaning, I showed code for moving and stopping code while facing upward, but I have code for down, left, and right, which looks the same. Also, to have only 4 directions, I put this in:
function MainCharacter::updateMovement(%this)
{
// this is where I had the if(%this.moveLeft) and set the velocity
// as well as the animations for the various movements.
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}
So that's basically what I've been using, but it feels (and probably looks) really cluttered, as though I'm making it harder than it needs to be. Like I stated earlier, if you need the entire file of code, I'd be happy to put it up here, just let me know.
#5
function PlayerUp()
{
$MainCharacter.moveUp = true;
$MainCharacter.moveDown = false;
$MainCharacter.moveLeft = false;
$MainCharacter.moveRight = false;
$MainCharacter.updateMovement();
}
...so that whatever keys were currently pressed will now be ignored.
06/16/2009 (3:27 pm)
What I would recommend is that when a key is pressed, you immediately clear all of the other movement flags:function PlayerUp()
{
$MainCharacter.moveUp = true;
$MainCharacter.moveDown = false;
$MainCharacter.moveLeft = false;
$MainCharacter.moveRight = false;
$MainCharacter.updateMovement();
}
...so that whatever keys were currently pressed will now be ignored.
#6
06/16/2009 (7:42 pm)
Thank you very much! That is exactly what I was looking for!
Torque Owner Zurik Phillips
There are some movement behaviors there with great control.