Game Development Community

player animation in behavior

by rennie moffat · in Torque Game Builder · 09/21/2009 (1:04 pm) · 5 replies

edit (in essence, what I am trying to figure out here is the best way to link animations in or with behaviors.. or is this matter much smaller than i am making it)


Hi there,
I am building a game that needs player animation, but here is my conundrum. I am currently using a behavior for the movement and have simply added in what I think is the needed code to the behavior. I did this thinking it was the only way to link the requirements of knowing the YVelocity.


I added in an enableUpdateCallback to the onBehaviorAdd,
a onUpdate function with setCurrentAnimation,
and of course a setCurrentAnimation function


However, no dice. I have seen some things that lead me to believe animation calls in behaviors are not necessarily possible without some fidgeting. Attached is the full behavior, please if you have some insight, drop it off.

One thought is, do I need a behaviorField for an animation?

/if (!isObject(MovementBehavior))
{
   %template = new BehaviorTemplate(MovementBehavior);
  
   %template.friendlyName = "Movement Behavior";
   %template.behaviorType = "Movement Styles";
   %template.description  = "Shooter style movement control";
  
   %template.addBehaviorField(upKey, "Key to bind to upward movement", keybind, "keyboard up");
   %template.addBehaviorField(downKey, "Key to bind to downward movement", keybind, "keyboard down");
   %template.addBehaviorField(leftKey, "Key to bind to left movement", keybind, "keyboard left");
   %template.addBehaviorField(rightKey, "Key to bind to right movement", keybind, "keyboard right");	
   %template.addBehaviorField(spaceKey, "Key to bind fire of guns", keybind, "keyboard space");

  
   %template.addBehaviorField(verticalSpeed, "Speed when moving vertically", float, 20.0);
   %template.addBehaviorField(horizontalSpeed, "Speed when moving horizontally", float, 20.0);
}

function MovementBehavior::onBehaviorAdd(%this)
{
   if (!isObject(moveMap))
   return;
  
   %this.owner.enableUpdateCallback();
  
   // bind our keys to the keyboard
   moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
   moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %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);
  
   // set the default values to 0
   %this.up = 0;
   %this.down = 0;
   %this.left = 0;
   %this.right = 0;
}

function MovementBehavior::onBehaviorRemove(%this)
{
   if (!isObject(moveMap))
   return;
   %this.owner.disableUpdateCallback();
  
   // remove the keybinds
   moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
   moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 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.down = 0;
   %this.left = 0;
   %this.right = 0;
   
}
  
function MovementBehavior::updateMovement(%this)
{
   // make the player move
   %this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
   %this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
  
}
  

  
function MovementBehavior::moveUp(%this, %val)
{
   %this.up = %val;
   %this.updateMovement();
}
  
function MovementBehavior::moveDown(%this, %val)
{
   %this.down = %val;
   %this.updateMovement();
}
  
function MovementBehavior::moveLeft(%this, %val)
{
   %this.left = %val;
   %this.updateMovement();
}
  
function MovementBehavior::moveRight(%this, %val)
{
   %this.right = %val;
   %this.updateMovement();
}




function MovementBehavior::onUpdate(%this)
{
   %this.setCurrentAnimation();
}
  



function MovementBehavior::setCurrentAnimation(%this)
{
  
   %yVelocity = %this.getLinearVelocityY();

  
   
   if(%yVelocity < 0)
      {
         %this.playAnimation(PlayerUpAnimation);
         return;
      }
	  
	if(%yVelocity > 0)
	{
	%this.playAnimation(PlayerDownAnimation);
	return;
	}
	
	if (%yVelocity = 0)
	{
	%this.playAnimation(PlayerFlyAnimation);
	return;
	}  
	 
}

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
09/21/2009 (7:04 pm)
At a quick glance, I think your setCurrentAnimation function needs to references %this.owner, not just %this. So use %this.owner.playAnimation.
#2
09/21/2009 (7:21 pm)
hmm. makes sense but not working.


#3
09/21/2009 (11:40 pm)
IDK sorry, Im dealing with platformers so I wouldn't know but if your anamations arn't working then maybe its the Anamation name. When you make an anamation save it as what you made it in the behavior or else it'll be ExampleImageMapAnamation or somthing.
#4
09/22/2009 (12:06 am)
yah no,
I went over that, I actually, originally named it playerUpAnimation, but I think as a default TGB adds animation on as a type of extension. So I noticed it was called playerUpANimationAnimation, but I changed it and still notta.


:[hmmm.



#5
09/24/2009 (2:33 am)
It's goes somthing like this this code im tring to work to but this is just like a hint.

function Anamation::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_Wait")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Wait);
}}
else
{
%this.owner.playAnimation(Player_Wait);
}}}
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);
}
if(%this.owner.getAnimationName() $= "Player_Jump")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Jump);
}}
else
{
%this.owner.playAnimation(Player_Jump);
}}}
else
{
if(%YVelocity == 20)
{
%this.owner.playAnimation(Player_Jump);
}