Game Development Community

Help with *Animations

by Edel · in Torque Game Builder · 11/10/2007 (5:57 pm) · 11 replies

So Im going along with the Mini Platformer Tutorial and the player runs and jumps; but now I have a punch and a kick (5 frames each). My question is how do I play the FULL five frames? When I press say... the punch button, all I get is the first frame?? Can somebody tell me how to correctly use the animation methods? Mine looks like this:

***// Called by onUpdate().***
function platformerControlsBehavior::AnimFago(%this)
{


if(%this.Duck == true)
{
%this.owner.setLinearVelocityX(0);


%this.owner.playAnimation(FagoPunch);

}//End if

}//End AnimFago

If anybody can help me out... THANK YOU!!

#1
11/10/2007 (8:37 pm)
Nevermind... I figured it out:

//////////////////////////////////////////////////////////////////////
if(%this.Duck == true)
{

%this.owner.setLinearVelocityX(0);
if(%this.owner.getAnimationName() $= "FagoDuck")
{
if(%this.owner.getIsAnimationFinished())
%this.owner.playAnimation(FagoDuck);
}
else
%this.owner.playAnimation(FagoDuck);




}//end if

** now if I can manage to only play this animation ONE TIME!!!! All it does is loop!!!
#2
11/11/2007 (12:25 am)
Dont remember the code right now, but theres a command to make a non-loopin animation... take a look at the reference guide.
#3
11/11/2007 (5:56 pm)
Im trying the reference docs...but still I cant figure out the best way of setting up player animation controls!! Seems with the reference Im still guessing... lol... The docs are lacking!
If they would only provide a bit of an example on how to implement the animation methods or all methods for that matter, it would make things a lot easier!
#4
11/11/2007 (6:13 pm)
Edel, I suggest you look at some of the example games. Also use the TDN wiki to find examples.
#5
11/11/2007 (6:20 pm)
One of the best methods is where you only change the animation when you want to to change, not every frame.

Lets say you have 2 states where 1. Player is idle and 2. Player is running.

function PlayerClass::onUpdate(%this)
{
	%newState = %this.getState();
	if (%newState)
	{
		%this.CurrentState = %newState;
		%this.playAnimation(%this.CurrentState);
	}
}

function PlayerClass::getState(%this)
{
	if (%this.CurrentState $= "IdleAnimation")
	{
		if (%this.getLinearVelocityX() != 0)
			return "RunningAnimation";
	}

	if (%this.CurrentState $= "RunningAnimation")
	{
		if (%this.getLinearVelocityX() == 0)
			return "IdleAnimation";
	}

	return 0;
}
#6
11/11/2007 (6:58 pm)
Thank you Phillip, I will try this.
#7
11/11/2007 (7:59 pm)
Well Phillip, here is what I got... trying to make your code tip work. No Luck!! Pretty funny. Im a good at programing... but this Torque script is MADNESS...Ha... I dont think I have -CurrentState- set properly? You can see that all I'm trying to do here is setup proper animations. Punch, Duck, Death and so on but I'm having such a hard time. There is an outdated tutorial on the TDN wiki... but since its outdated I wanted to stay away from that one one. The newer tutorials Fish, Checker, Wack a Mole, dont seem to have any -animation intensive- examples. Or at least dont seem to give me much to work with in terms of animation scripting.



if(!isObject(platformerControlsBehavior))
{
   %template = new BehaviorTemplate(platformerControlsBehavior);
   
   %template.friendlyName = "Platformer test 1.0";
   %template.behaviorType = "Input";
   %template.description  = "Movement control for the Platformer Tutorial";
   
   %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(duckKey,  "Key to Bind to duck movement",    keybind, "Duck" ); //my keys
   %template.addBehaviorField(punchKey,  "Key to Bind to punch movement",  keybind, "Punch"); //my keys


   %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);
   moveMap.bindObj(getWord(%this.duckKey,   0), getWord(%this.duckKey,  1), "duck"     , %this); //my keys
   moveMap.bindObj(getWord(%this.punchKey,  0), getWord(%this.punchKey, 1), "punch"    , %this); //my keys

   %this.left   = 0;
   %this.right  = 0;
   %this.Duck   = 0;
   %this.Punch  = 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;
   //%this.onUpdate();
}

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


}

function platformerControlsBehavior::punch(%this, %val)
{
  %this.Punch = %val;
  //%this.onUpdate();
  
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////



function platformerControlsBehavior::onUpdate(%this)
{
  ////////////////////////////////////////////////////////////////
      %newSpeed = (%this.right - %this.left) * %this.runSpeed;
      %this.Owner.setLinearVelocityX(%newSpeed);

	%newState = %this.getState();
	if (%newState)
	{
		%this.CurrentState = %newState;
		%this.owner.playAnimation(%this.CurrentState);
	}
}

function platformerControlsBehavior::getState(%this)
{

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


	if (%this.CurrentState $= "FagoStand")
	{
		if (%xVelocity != 0)
			return "FagoRun";
	}

	if (%this.CurrentState $= "FagoRun")
	{
		if (%xVelocity == 0)
			return "FagoStand";
	}

	return 0;
}
#8
11/11/2007 (8:54 pm)
Sorry, I forgot to mention that you need to add an initial state. Just put the following line of code in your "onBehaviorAdd" function:

%this.CurrentState = "FagoStand";

Do you understand what my code is trying to do? You should throw in a few "echo()" calls here and there to make sure its doing what you intend it to do. Good luck!
#9
11/12/2007 (3:33 pm)
Thanks Phillip, But I am stomped!! this code should start on the players Idle animation ("FagoStand")... but it simply wont work. I make some 'checks' by doing this :

function platformerControlsBehavior::onUpdate(%this)
{
  ////////////////////////////////////////////////////////////////
  


      %newSpeed = (%this.right - %this.left) * %this.runSpeed;
      %this.Owner.setLinearVelocityX(%newSpeed);

      %newState = %this.getState();

	if (true)  // <----************************************************
	{
		%this.CurrentState = %this.newState;
		%this.owner.playAnimation(%this.CurrentState);
	}
}

if(!isObject(platformerControlsBehavior))
{
   %template = new BehaviorTemplate(platformerControlsBehavior);
   
   %template.friendlyName = "Platformer test 1.0";
   %template.behaviorType = "Input";
   %template.description  = "Movement control for the Platformer Tutorial";
   
   %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(duckKey,  "Key to Bind to duck movement",    keybind, "Duck" ); //my keys
   %template.addBehaviorField(punchKey,  "Key to Bind to punch movement",  keybind, "Punch"); //my keys


   %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);
   moveMap.bindObj(getWord(%this.duckKey,   0), getWord(%this.duckKey,  1), "duck"     , %this); //my keys
   moveMap.bindObj(getWord(%this.punchKey,  0), getWord(%this.punchKey, 1), "punch"    , %this); //my keys

   %this.left   = 0;
   %this.right  = 0;
   %this.Duck   = 0;
   %this.Punch  = 0;
   %this.CurrentState = "FagoStand";



	// 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;
   //%this.onUpdate();
}

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


}

function platformerControlsBehavior::punch(%this, %val)
{
  %this.Punch = %val;
  //%this.onUpdate();
  
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////



function platformerControlsBehavior::onUpdate(%this)
{
  ////////////////////////////////////////////////////////////////
  


      %newSpeed = (%this.right - %this.left) * %this.runSpeed;
      %this.Owner.setLinearVelocityX(%newSpeed);

      %newState = %this.getState();

	if(%newState)
	{
		%this.CurrentState = %this.newState;
		%this.owner.playAnimation(%this.CurrentState);
	}
}

function platformerControlsBehavior::getState(%this)
{

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


	if (%this.CurrentState $= "FagoStand")
	{
		if (%xVelocity != 0)
			return "FagoRun";
	}

	if (%this.CurrentState $= "FagoRun")
	{
		if (%xVelocity == 0)
			return "FagoStand";
	}

	return 0;
}

I think the problem might be I'm doing somthing wrong in -- getState(%this) function? Anyways I'll keep trying. Thank you very much though.
#10
11/12/2007 (6:19 pm)
if (true)	{		%this.CurrentState = %this.newState;		%this.owner.playAnimation(%this.CurrentState);	}

You have the above listed there, you should not have "if (true)" otherwise this will always be true! It should be "if (%newState)" or "if (%newState !$= "0")".
#11
11/12/2007 (8:40 pm)
Sorry I had "if(%newState)" before, but I simply set the condition to true just to do some checking. But even still no such luck. Im going through this code just to make sure I have not misspelled anything...