0
\$\begingroup\$

I'm trying to get my figure to look in the right directions, based on the input of the joystick. So if I tilt left it looks left and the left animation is used, if I used right, it looks right and right animation is used, if up, then up, down, down and so on. I just get animation for front and back. Also if I press up I see the back of the figure correctly, but it won't go back into the original state when I don't press up anymore.

-(void)applyJoystick:(SneakyJoystick *)aJoystick forTimeDelta:(float)
deltaTime {
    CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 128.0f);
    CGPoint oldPosition = [self position];
    CGPoint newPosition =
    ccp(oldPosition.x + scaledVelocity.x * deltaTime,
    oldPosition.y + scaledVelocity.y * deltaTime);
    [self setPosition:newPosition];
    id action = nil;
   int extra = 50;
if ((int) aJoystick.degrees > 180 - extra && aJoystick.degrees < 180 + extra) {
action = [CCAnimate actionWithAnimation:walkingAnimLeft restoreOriginalFrame:NO];

} else if ((int) aJoystick.degrees > 360 - extra && aJoystick.degrees < 360 + extra) {
    action = [CCAnimate actionWithAnimation:walkingAnimRight restoreOriginalFrame:NO];
}

else if ((int) aJoystick.degrees > 90 - extra && aJoystick.degrees < 90 + extra) {
    action = [CCAnimate actionWithAnimation:walkingAnimBack restoreOriginalFrame:NO];

}
 else if ((int) aJoystick.degrees > 270 - extra && aJoystick.degrees < 270 + extra) {
action = [CCAnimate actionWithAnimation:walkingAnimFront restoreOriginalFrame:NO];
}

if (action != nil) {
[self runAction:action];
        }
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Cue Samuel Jackson: Indents, motherflipper, do you use them!? \$\endgroup\$ Commented Jun 14, 2012 at 13:41
  • \$\begingroup\$ Could you explain that further? :) \$\endgroup\$
    – Blade
    Commented Jun 14, 2012 at 17:14
  • \$\begingroup\$ And now I get it: Yes I tried with flipX, which didn't work as intended. It flips the y sprite/animation, but not the x sprite. It doesn't even display that one. \$\endgroup\$
    – Blade
    Commented Jun 14, 2012 at 19:46

1 Answer 1

0
\$\begingroup\$

My approach was more or less the right one. But the direction cue had to go somewhere completely different, and also had to be changed a little. It is now in

-(void)updateStateWithDeltaTime:(ccTime)deltaTime
andListOfGameObjects:(CCArray*)listOfGameObjects {
 if (joystick.degrees > 60 && joystick.degrees < 120) {
        if (self.characterState != kStateWalkingBack) 
            [self changeState:kStateWalkingBack];

    } else if (joystick.degrees > 211 && joystick.degrees < 300) {
        if (self.characterState != kStateWalkingFront) 
            [self changeState:kStateWalkingFront];

    } else if (joystick.degrees > 301 && joystick.degrees < 360){
    if (self.characterState != kStateWalkingRight)
            [self changeState:kStateWalkingRight];

    } else if (joystick.degrees > 121 && joystick.degrees < 210) {
        if (self.characterState != kStateWalkingLeft)
            [self changeState:kStateWalkingLeft];
    }
        [self applyJoystick:joystick 
               forTimeDelta:deltaTime];
    }
}

If someone has further questions about this, don't hesitate to ask :)

\$\endgroup\$
2
  • \$\begingroup\$ How does the changeState thing change the sprite animation? I'm reading the cocos2d book and the guy says to try and NOT use Actions as much as possible, so I'm scouring the web to see how I animate a 'walking in various directions during movement' sprite. \$\endgroup\$
    – MetaGuru
    Commented Oct 25, 2012 at 22:32
  • \$\begingroup\$ Hey, I had to put my cocos2d project on hold, because I programmed an app for a client. However I still try to answer your question, even though I am a bit rusty at that point: What I am doing here is to check, when the joystick is for example pointed upward, if the figure is looking/walking upward. If not, I change it to the kStateWalkingFront. I read the Ray Wenderlich book, where it is explained pretty good. If you use what I have written above, you need to define the kStates somewhere and also put the animation or the link to the animation after the definition. Works fine then :) \$\endgroup\$
    – Blade
    Commented Oct 28, 2012 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .