Game Development Community

TGB NEWB QUESTION

by Trenton Moore · in Torque Game Builder · 11/04/2009 (6:46 am) · 1 replies

Im attempting to build a platform game using TGB

The only problem is that i cannot get the player animation state to change when he jumps


here is the code im using


if (!isObject(MovementControlsBehavior))
{
%template = new BehaviorTemplate(MovementControlsBehavior);

%template.friendlyName = "Movement Controls";
%template.behaviorType = "Input";
%template.description = "Platformer style movement control";

%template.addBehaviorField(upKey, "Key to bind to Jump", keybind, "keyboard up");
%template.addBehaviorField(leftKey, "Key to bind to Run left", keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to Run right", keybind, "keyboard right");
%template.addBehaviorField(space, "Key to bind to Cast Spell", keybind, "keyboard space");
%template.addBehaviorField(a, "Key to bind to Change Spell Left", keybind, "a");

%template.addBehaviorField(Health, "Health Stat", float, 50.0);
%template.addBehaviorField(Defense, "Defense Stat", float, 0.0);
}

function MovementControlsBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
%this.owner.enableUpdateCallback();
%this.owner.Health = %this.health;
%this.owner.Defense = %this.defense;
$Spell = 1;
%this.owner.spellAble = true;

moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "MoveUp", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "MoveLeft", %this);
moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "MoveRight", %this);
moveMap.bindObj(getWord(%this.space, 0), getWord(%this.space, 1), "PressSpace", %this);
moveMap.bindObj(getWord(%this.a, 0), getWord(%this.a, 1), "PressA", %this);
}

function MovementControlsBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;

%this.owner.disableUpdateCallback();

moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);

%this.up = 0;
%this.left = 0;
%this.right = 0;
%this.space = 0;
}

function MovementControlsBehavior::moveUp(%this, %val)
{
%this.up = %val;
}

function MovementControlsBehavior::moveLeft(%this, %val)
{
if(%val < 0.20 && %val > -0.20) //used with joystick
{
%val = 0;
}
%this.left = %val * 28; // makes the playerfs horizontal speed to 28.
}

function MovementControlsBehavior::moveRight(%this, %val)
{
if(%val < 0.20 && %val > -0.20) //used with joystick
{
%val = 0;
}
%this.right = %val * 28;// makes the playerfs horizontal speed to 28.
}

function MovementControlsBehavior::PressSpace(%this, %val)
{
if(%val == 1)
{
%this.space = %val;
}}

function MovementControlsBehavior::OnUpdate(%this)
{
if(%this.owner.getAnimationName() $= "Player_TakeDamage" || %this.owner.getAnimationName() $= "Player_Cast" || %this.owner.getAnimationName() $= "Player_Die" || %this.owner.getAnimationName() $= "Player_JumpUp" || %this.owner.getAnimationName() $= "Player_JumpDown")
{
%this.owner.setLinearVelocityX(0);
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Wait);
}

else
{
if(%this.owner.getLinearVelocityY() < -1)
{



//This code makes the one way collisions
%this.owner.setCollisionActive(false,false);
}
else
{
%this.owner.setCollisionActive(true,true);
}
%this.owner.SetLinearVelocityX(%this.right - %this.left);
if(%this.up == 1)
{
if(!%this.owner.airborne)
{
%this.owner.setLinearVelocityY(-100);
%this.owner.airborne = true;
}}
%yVelocity = %this.owner.getLinearVelocityY();
if(%this.owner.airborne)
{
%this.owner.setLinearVelocityY(0);
%collision = %this.owner.castCollision(0.005);
if(%collision !$= "")
%this.owner.setLinearVelocityX(0);
}
%this.owner.setLinearVelocityY(70);
%collision = %this.owner.castCollision(0.005);

if(%collision $= "")
{
%this.owner.airborne = true;
%this.owner.setConstantForceY(70);
}
else if(%yVelocity < 0 && %this.owner.airborne)
{
%this.owner.setConstantForceY(70);
}
else
{
%this.owner.airborne = false;
%this.owner.setConstantForceY(0);
}
%this.owner.setLinearVelocityY(%yVelocity);
%this.Animation();
}}

function MovementControlsBehavior::Animation(%this)
{
%xVelocity = %this.owner.getLinearVelocityX();
%yVelocity = %this.owner.getLinearVelocityY();
if(%xVelocity > 0)
{
%this.owner.setFlip(false, false);
}
else if(%xVelocity < 0)
{
%this.owner.setFlip(true, false);
}
if(%this.owner.airborne)
{
if(%yVelocity > 0)
{
if(%this.owner.getAnimationName() $= "Player_JumpUp")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Wait);
}}
else
{
%this.owner.playAnimation(Player_JumpUp);
}}}
else
{
if(%xVelocity == 0)
{
%this.owner.playAnimation(Player_Wait);
}
else
{
if(%this.owner.getAnimationName() $= "Player_Walk")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Walk);
}}
else
{
%this.owner.playAnimation(Player_Walk);
}}}


}



Player states are Player_Walk, Player_Wait, Player_JumpUp and PlayerJumpDown


any suggestions?

About the author

Recent Threads


#1
11/06/2009 (5:44 am)
I had the same problem with this behavior you could try this one it works too. :)

if(!isObject(platformerControlsBehavior))
{
   %template = new BehaviorTemplate(platformerControlsBehavior);
   
   %template.friendlyName = "Platformer Tutorial Controls";
   %template.behaviorType = "Input";
   %template.description  = "Movement control for a Platformer";
   
   %template.addBehaviorField(jumpKey, "Key to bind to jump movement", keybind, "Space");
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "Left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "Right");
   
   %template.addBehaviorField(runSpeed, "Speed when running", float, 60.0);
   %template.addBehaviorField(jumpSpeed, "Speed when jumping", float, 150.0);
}

function platformerControlsBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
      return;
   
   // bind our keys on the keyboard
   moveMap.bindObj(getWord(%this.jumpKey, 0), getWord(%this.jumpKey, 1), "jump", %this);
   moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
   moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);

   %this.left = 0;
   %this.right = 0;
	
	// Enable the update callback.
	%this.owner.enableUpdateCallback();
	
	// Set some collision stuffs.
	%this.owner.setCollisionMaxIterations(2);
}

// It appears that these are called once every tick...?
function platformerControlsBehavior::jump(%this, %val)
{
	if(!%this.owner.airborne)
		%this.launch(-%this.jumpSpeed);
}

function platformerControlsBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
}

function platformerControlsBehavior::moveRight(%this, %val)
{
   %this.right = %val;
}

function platformerControlsBehavior::OnUpdate(%this)
{
	%this.updateHorizontal();
	%this.updateVertical();
	%this.setCurrentAnimation();
}

function platformerControlsBehavior::launch(%this, %speed)
{
	%this.owner.setLinearVelocityY(%speed);
	%this.owner.airborne = true;
}

// Called by onUpdate().
function platformerControlsBehavior::updateHorizontal(%this)
{
   // figure out what speed in what direction we are moving based on key input
	%newSpeed = (%this.right - %this.left) * %this.runSpeed;
   %this.owner.setLinearVelocityX(%newSpeed);
	
	// Clamp Vertical velocity on stop movement for up-ramps
	if(!%this.owner.airborne)
		%this.owner.setLinearVelocityY(0);
}

// Called by onUpdate().
function platformerControlsBehavior::updateVertical(%this)
{
	%yVelocity = %this.owner.getLinearVelocityY();
	
	if(%this.owner.airborne)
	{
		%this.owner.setLinearVelocityY(0);
		%collision = %this.owner.castCollision(0.005);
		
		if(%collision !$= "")
			%this.owner.setLinearVelocityX(0);
	}
	
	%this.owner.setLinearVelocityY(100);
	%collision = %this.owner.castCollision(0.005);
	
	if(%collision $= "")
	{
		%this.owner.airborne = true;
		%this.owner.setConstantForceY(100);
	}
	else if(%yVelocity < 0 && %this.owner.airborne)
	{
		%this.owner.setConstantForceY(100);
	}
	else
	{
		%this.owner.airborne = false;
		%this.owner.setConstantForceY(0);
	}
	
	%this.owner.setLinearVelocityY(%yVelocity);
}

// Called by onUpdate().
function platformerControlsBehavior::setCurrentAnimation(%this)
{
	%xVelocity = %this.owner.getLinearVelocityX();
	%yVelocity = %this.owner.getLinearVelocityY();
	
	// Set the flip of the avatar.
	if(%xVelocity > 0)
	{
		%this.owner.setFlip(false, false);
	}
	else if(%xVelocity < 0)
	{
		%this.owner.setFlip(true, false);
	}
	
	// Animations.
	if(%this.owner.airborne)
	{
		if(%yVelocity < 0)
			%this.owner.playAnimation(playerJumpUp);
		else
			%this.owner.playAnimation(playerJumpDown);
	}
	else
	{
		if(%xVelocity == 0)
		{
				%this.owner.playAnimation(playerStand);
		}
		else
		{
			if(%this.owner.getAnimationName() $= "playerRun")
			{
				if(%this.owner.getIsAnimationFinished())
					%this.owner.playAnimation(playerRun);
			}
			else
				%this.owner.playAnimation(playerRun);
		}
	}
}

Also dose it play when you move right or left. With this one you need to name your anamations PlayerStand,PlayerRun,PlayerJumpUp,and PlayerJumpDown hope this helped :)