Game Development Community

I Can not get my platformer player to Duck

by brandonrandon · in Torque Game Builder · 09/27/2009 (10:58 am) · 2 replies

I am currently building a platformer based off of the platformer tutorial but with some added features, different sprites, animations and environment. The problem I am having is this, I have created a duck animation that will be called when the "s" key is pressed on the keyboard. I call this animation just like I do the others but for some reason it will not execute.

Here is my code.

function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;

moveMap.bindCmd(keyboard, "a", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "d", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "w", "playerJump();", "");
moveMap.bindCmd(keyboard, "s", "playerDuck();", "playerStopDuck");

%this.enableUpdateCallback();
}
function playerLeft()
{
$player.moveLeft = true;
}

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

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

function playerRightStop()
{
$player.moveRight = false;
}

function playerJump()
{
if(!$player.airborne)
{
$player.setLinearVelocityY(-150);
$player.airborne = true;
}
}
//Player Duck functions
function playerDuck()
{
$player.Duck = true;
}
function playerDuckStop()
{
$player.Duck = false;
}

//.............

function playerClass::onUpdate(%this)
{
%this.updateHorizontal();
%this.updateVertical();
%this.setCurrentAnimation();
}
function playerClass::setCurrentAnimation(%this)
{
%xVelocity = %this.getLinearVelocityX();
%yVelocity = %this.getLinearVelocityY();

if (%xVelocity > 0)
{
%this.setFlip(false, false);
}
else if (%xVelocity < 0)
{
%this.setFlip(true, false);
}

if (%this.airborne)
{
if(%yVelocity < 0)
{
%this.playAnimation(KadinJumpAnimation);
return;
}else
{
%this.playAnimation(KadinJumpDuckAnimation);
return;
}
}
//Execute player duck animation
if ($player.Duck == true)
{
%this.playAnimation(KadinDuckAnimation);
}

if ($player.moveLeft == true || $player.moveRight == true)
{
if (%this.getAnimationName() $= "KadinRun1Animation")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(KadinRun1Animation);
return;
}
}else
{
%this.playAnimation(KadinRun1Animation);
}
}else
{
%this.playAnimation(KadinExtendedIdleAnimation);
}
}

About the author

Recent Threads


#1
09/30/2009 (1:35 pm)
Quote:moveMap.bindCmd(keyboard, "s", "playerDuck();", "playerStopDuck");

In the code fragment above, replace "playerStopDuck" with "playerStopDuck();"
#2
10/02/2009 (10:49 pm)
Thanks, the fix did not get the action in the game to work but hopefully it helps to lead me to the answer.