Game Development Community

Help in Torque 2D? ASAP (it's for school)

by Chris Faraji · in Torque Game Builder · 03/14/2010 (8:23 am) · 20 replies

That is my code. I am starting to make a simple platformer/collection game for a final project. When my character RUNS or JUMPS, the animation will stay as either or. It never reverts back to the idle animation. How do I make it so it will always go back to IDLE and so when I press space to jump, I can only press it once instead of over and over?

function Player::onLevelLoaded(%this, %scenegraph)
{
//GLOBAL for player
$Player = %this;

//CONTROLS
moveMap.bindCmd(keyboard, "right", "PlayerRight();", "PlayerRightStop();");
moveMap.bindCmd(keyboard, "left", "PlayerLeft();", "PlayerLeftStop();");
moveMap.bindCmd(keyboard, "space", "PlayerJump();", "PlayerJumpStop();");
}

//MOVE, and STOP
function PlayerRight()
{
$Player.playAnimation(Player_RUNAnimation);
$Player.setLinearVelocityX(20);
$Player.setFlipX(true);
}

bool move = true;

function PlayerLeft()
{
$Player.playAnimation(Player_RUNAnimation);
$Player.setLinearVelocityX(-20);
$Player.setFlipX(false);
}

function PlayerJump()
{
$Player.setLinearVelocityY(-50);
$Player.playAnimation(Player_JumpAnimation);
}
//--------------------------------------
function PlayerLeftStop()
{
%this.Player.playAnimation(IdlePlayerAnimation);
$Player.setLinearVelocityX(0);
}

function PlayerRightStop()
{
%this.Player.playAnimation(IdlePlayerAnimation);
$Player.setLinearVelocityX(0);
}


function PlayerJumpStop()
{
$Player.setLinearVelocityY(0);
%this.Player.playAnimation(IdlePlayerAnimation);
}


About the author

College student in Game Programming, hoping to make it somewhere. I have some great ideas, and want to try and show the world what I've got.


#1
03/14/2010 (8:40 am)
In the stop functions you a referring to the player object as %this.Player. Change it to $Player instead.
#2
03/14/2010 (8:45 am)
so have it say "$Player.playAnimation"?
#3
03/14/2010 (8:50 am)
Look at what you wrote in the PlayerLeft function:
$Player.playAnimation(Player_RUNAnimation);

The run animation works, yes? So do the same for the idle animation.
#4
03/14/2010 (8:52 am)
I did that, but it still isn't working :/
#5
03/14/2010 (9:10 am)
Between the PlayerRight and PlayerLeft functions you have a bool move = true;. Get rid of that as it is giving you a parse error and the modified script is not compiling. You should get into the habit of checking the console (access it in game with the ~ key on American keyboards) regularly to see if you have any script errors.
#6
03/14/2010 (9:15 am)
I got it! Apparently I just had something out of order. thanks a lot though Mike, you were a huge help
#7
03/14/2010 (9:19 am)
You're welcome. :)
#8
03/14/2010 (9:27 am)
Actually, I was also wondering:

How can I make the code, so that you can only press space one time to jump, and that the jump animation lasts until you touch ground, as opposed to as soon as i release the button?
#9
03/14/2010 (10:22 am)
Well, if you don't want the idle animation to play when you release the button, remove it from those functions. Then ask yourself, how does the engine know when the player object touches the ground object? Normally it doesn't know, unless you turn on the collision properties for both objects. So in script you can use the onCollision callback to specify what should happen when two objects collide, and in your case you would like the player object to play the idle animation.
#10
03/14/2010 (10:27 am)
hmm...

so like

function Player::onCollision
{
$Player.playAnimation();
}

?
#11
03/14/2010 (11:00 am)
As basic theory, yes you are on the right track. But if you type in the code exactly as you posted, then no it will not work - you're missing a couple things.

I know I'm being a bit vague, but hopefully you can understand it's not my job to write your final project for you. :)

TGB comes with a great reference. In the online or offline documentation, look under Reference->TGB Reference->t2dSceneObject->Callbacks->onCollision to understand how the function is written and what parameters you can use.
#12
03/14/2010 (11:14 am)
I completely understand :) thank you though. If I am unable to figure it out, would you be able to lend a hand?
#13
03/14/2010 (11:47 am)
Yes of course, I have no problem helping out. I'm sure you've heard of the Chinese proverb: Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. With TGB, I'd prefer obviously to help in the "teaching" aspect instead of just handing out code snippets.
#14
03/14/2010 (1:31 pm)
Umm, hey Mike, I decided for now since I cannot get any of the jumping to work, I'll exclude it for Version 1.0 however I removed the code, saved my game and files and closed TGB but I can still jump?
#15
03/14/2010 (2:05 pm)
Ok, what happens if you delete (or move to another folder) the script's .cs.dso file? When you write a script file, the engine compiles it at runtime into a .cs.dso file. If you make changes to the script file and introduce an error into it, TGB will not compile the edited script and instead just run the older, correctly working .dso file which means you won't see any changes in game.
#16
03/14/2010 (4:36 pm)
Ohh crap crap crap! I accidentally deleted those .dso files and now nothing works D:

#17
03/14/2010 (11:03 pm)
It's okay to delete .dso files. You just don't want to delete the .cs files.

If nothing is working now, then Mike's assumption is correct: you are getting compile errors and no new .dso files will be created.

Check the console after you run the game (Ctrl+~) to find your compile errors.
#18
03/16/2010 (5:38 pm)
function Player::onLevelLoaded(%this, %scenegraph)
{
//GLOBAL for player
$Player = %this;

//CONTROLS
moveMap.bindCmd(keyboard, "right", "PlayerRight();", "PlayerRightStop();");
moveMap.bindCmd(keyboard, "left", "PlayerLeft();", "PlayerLeftStop();");
}

//MOVE, and STOP
function PlayerRight()
{
$Player.moveRight = true;
$Player.updateMovement();
}

function PlayerLeft()
{
$Player.moveLeft = true;
$Player.updateMovement();
}
//--------------------------------------
function PlayerLeftStop()
{
$Player.moveLeft = false;
$Player.updateMovement();
}

function PlayerRightStop()
{
$Player.moveRight = false;
$Player.updateMovement();
}
//-----UPDATE----
function Player::updateMovement(%this)
{
if(%this.moveLeft)
{
$Player.playAnimation(PrinnyWalk);
$Player.setLinearVelocityX(-20);
$Player.setFlipX(false);
}

if(%this.moveRight)
{
$Player.playAnimation(PrinnyWalk);
$Player.setLinearVelocityX(20);
$Player.setFlipX(true);
}

if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}


It says I am getting a parse error on line 55. Line 55, is the last bracket.
#19
03/16/2010 (7:02 pm)
You're missing a close curly brace.
#20
03/16/2010 (7:47 pm)
I found it a little while ago thanks though!! It was driving me insane haha