Game Development Community

Trouble adding an animation to the SpaceShooter behavior

by Derek Traver · in Torque Game Builder · 12/09/2009 (5:58 pm) · 5 replies

Hey everyone,

I'm very new to this, and have just been toying around, so bare with me.

Heres my problem:

I've been playing with the SpaceShooter example behavior from the TGB documentation, and I'm trying to make it so when you move the character it triggers an animation, and stops when you stop moving. I have a 6 frame movement animation for the character, named "player_move". My standing still and default sprite is "player_idle".

Adding the following code worked, sort of, except when I am moving the character it only shows the first frame of my animation, it doesn't show all 6 looped (and I do have it set to cycle on the animation).

function ShipMovementBehavior::updateMovement(%this)
{
	%this.owner.rotateTo(%this.TargetAngle, %this.turnspeed);
	
	if(%this.left==0 && %this.right==0 && %this.up==0 && %this.down==0)
	{
		%idle="idle";
	}
	else{
		%idle="move";
	}
	
	if(%this.left + %this.right + %this.up + %this.down) // checks for input
	{
		%this.owner.setAnimation("player_" @ %idle );
		%this.owner.setImpulseForcePolar(%this.TargetAngle, %this.acceleration);
		//sets one time force in the direction of keys pressed
	}
}

What am I doing wrong?

#1
12/10/2009 (3:08 am)
At first glance, this code looks correct.

I'd add an echo inside your last if-statement to see if this function is getting called over and over.

if(%this.left + %this.right + %this.up + %this.down) // checks for input
{
  echo( "Setting animation to player_" @ %idle );
  %this.owner.setAnimation("player_" @ %idle );
  %this.owner.setImpulseForcePolar(%this.TargetAngle, %this.acceleration);
}
#2
12/10/2009 (3:12 am)
I just saw another error (but not the one you are seeing).

It looks like "setAnimation" will only be called if you are moving. If there is no input, there is no "setAnimation".

You might just try a complete re-write:
function ShipMovementBehavior::updateMovement( %this )
{
  %this.owner.rotateTo( %this.TargetAngle, %this.turnspeed );
	
  if( %this.left == 0 && %this.right == 0 && %this.up == 0 && %this.down == 0 )
  {
    %this.owner.setAnimation( "player_idle" );
  }
  else
  {
    %this.owner.setAnimation( "player_move" );
    %this.owner.setImpulseForcePolar( %this.TargetAngle, %this.acceleration );
  }
}
#3
12/10/2009 (10:58 am)
Thanks William... Your code is definitely cleaner.

It still doesn't make the animation to play when the character is moving though... only the first frame, it looks like he is skating.

I tried removing the
%this.owner.setAnimation( "player_idle" );
from the first if block just to see if that was getting triggered repeated, stopping the "move" animation (thinking it would trigger the "move" animation perpetually).

It does trigger the move animation perpetually, but still, the animation only fully plays when I stop giving input. So when the sprite is actually moving he's still frozen in the first frame.

Is it something with setImpulseForcePolar? Is it only showing the first frame of animation because setImpulseForcePolar only actually applies force for an instant, rather than constant velocity?

I tried replacing this line:

%this.owner.setImpulseForcePolar( %this.TargetAngle, %this.acceleration );

with this:

%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);  
   %this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);

and adding horizontalSpeed and verticalSpeed as behaviorFields, and it animates perfectly, idling when theres no input and showing the "move" animation when there is input.

But this control scheme doesn't feel right, I much prefer how the character slides to a stop with setImpulseForcePolar and damping.

In case you were wondering, the effect I am pretty much going for is like the one from Larva Mortus.
#4
12/10/2009 (5:37 pm)
It sounds like your key down behavior is repeatedly calling the "updateMovement" function. You might try checking to see if the key state changed before calling that function.

I find it hard to believe that the setImpulseForcePolar is causing the problem, as I'm certain that it doesn't affect animations at all. I find it weird that changing it to linear velocities worked.
#5
05/29/2011 (1:46 pm)
is your image an animation image or just a still image, did you do the create animation image after you made your animation a cell image?