Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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];
        }
    }
}
share|improve this question
2  
Cue Samuel Jackson: Indents, motherflipper, do you use them!? –  Jonathan Hobbs Jun 14 '12 at 13:41
    
Could you explain that further? :) –  Blade Jun 14 '12 at 17:14
    
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. –  Blade Jun 14 '12 at 19:46

1 Answer 1

up vote 0 down vote accepted

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 :)

share|improve this answer
    
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. –  ioSamurai Oct 25 '12 at 22:32
    
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 :) –  Blade Oct 28 '12 at 7:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.