Cocos2D: Change animation based on joystick direction
- by Blade
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];
}
}
}