Search code examples
objective-cxcodecocos2d-iphonejoystick

cocos2d: Trigger code on SneakyJoystick down movement


I have a game in progress where the player can only move left and right, but at certain places on the map they can push up or down to go to a new scene in those directions (such as through a door in a back wall).

How can I set up my SneakyJoystick to trigger some code when the user pushes down? At the moment it's doing the below to move my player left/right, I want this to continue, but (for now) for a CCLOG to execute when the player pushes the stick entirely up, or down. Any ideas?

- (void) applyJoystick:(SneakyJoystick *)aJoystick forTimeDelta:(float)deltaTime {
    CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 325.0f);
    CGPoint oldPosition = [self position];
    CGPoint newPosition = ccp(oldPosition.x + scaledVelocity.x * deltaTime, oldPosition.y);

    [self setPosition:newPosition];
    if (oldPosition.x > newPosition.x) {
        self.flipX = YES;
    } else if (oldPosition.x == newPosition.x) {
        // Intentionally do nothing to preserve orientation at start of scene!
    } else {
        self.flipX = NO;
    }
}

Solution

  • int extra = 20;
    if ((int) aJoystick.degrees > 270 - extra && aJoystick.degrees < 270 + extra) {
        // Joystick button seems to be pointing down
    }
    

    I think that works. I don't remember well. I think 270 degrees is perfectly down. I am checking if the direction degree is within a certain range (270 - 20 and 270 + 20).