views:

268

answers:

2

Hey guys!

I am trying to run a "Walk" style animation on my main game sprite. The animations work fine, and my sprite is hooked up to my joystick all fine and dandy.

However, I think where I setup the call for my walk animations are wrong. Because everytime the sprite is moving, the animation stops.

I know putting the animation in such a weak if statement is probably bad, but please tell me how I could get my sprite to animate properly while it is being moved by the joystick.

The sprite faces the right direction, so I can tell the action's first frame is being called, however, it doesn't animate until I stop touching my joystick.

Here is How I call the action:

//WALK LEFT

if (joypadCap.position.x <= 69 /*&& joypadCap.position.y < && joypadCap.position.y > >40 */ )
{

  [tjSprite runAction:walkLeft];  

};

//WALK RIGHT

if ( joypadCap.position.x >= 71 /* && joypadCap.position.y < 100 && >joypadCap.position.y > 40 */) {

  [tjSprite runAction:walkRight];

};

THIS: is the how the joystick controls the character:

CGPoint newLocation = ccp(tjSprite.position.x - distance/8 * cosf(touchAngle),
tjSprite.position.y - distance/8 * sinf(touchAngle));
tjSprite.position = newLocation;

Please help. Any alternative ways to call the characters walk animation would be greatly appreciated!

A: 

The sprite faces the right direction, so I can tell the action's first frame is being called, however, it doesn't animate until I stop touching my joystick.

Based on the code you have supplied this actually makes sense. What your if statement is saying is anytime the joypadCap position is greater than 71 or less than 69 play an animation. This means your animations will try to play over and over again from the beginning everytime joypadCap's position falls in these ranges. I assume joypadCap is a measure of how much the joystick is being pressed?

Looks like you could use some additional state logic to determine what your character should be doing. Here is some pseudocode...

state current_state;

if (current_state != walking and joypadCap.position.x <= 69)
{
  current_state = walking;
  [tjSprite runAction:walkLeft];  
}    
else if (current_state != walking and joypadCap.position.x >= 71)
{
  current_state = walking;
  [tjSprite runAction:walkRight];  
}
else 
{
  current_state = idle;
  [tjSprite stopAllActions];
}

Keep in mind that is loose pseudocode. Not everything is syntactically correct but logically the idea is that you have a state variable to keep track of the characters current state which allows you so have your animations play one time only. Let me know if this helps and if you have any questions about my answer.

Rob Segal
Rob, I thank you so much for replying. I am so grateful.I tried your suggestion, and I got great results! However, this sprite will not animate while being moved, but will face the direction intended.If I comment out the last "else" statement where I stopAllActions on the sprite, it will animate AFTER it has been moved. The code I used is attached in the next answer. This in my mainGameLoop method.Thank you so much, please help if you can :c) I have a great story to tell.
maiko
A: 
int current_state;  



if (current_state != 1 && joypadCap.position.x <= 69)
{
    current_state = 1;
    [tjSprite runAction:walkLeft];  
}    
else if (current_state != 1 && joypadCap.position.x >= 71)
{
    current_state = 1;
    [tjSprite runAction:walkRight];  
}
else 
{
    current_state = 0;
    //[tjSprite stopAllActions];
};
maiko