kobold2d

How to implement Kobold2D KKInput gestureSwipeDirection to if else statements?


I'm finding it hard to learn from documentation on how I can use the Kobold2D KKInput gestureSwipeDirection to detect swipes left/right/up/down and have them carry out if else statements. Can anyone help by providing me with a sample code. Thanks

KKInput* input = [KKInput sharedInput];
KKSwipeGestureDirection dir = input.gestureSwipeDirection;
switch (dir)
{
    case KKSwipeGestureDirectionRight:
        // direction-specific code here
        break;
    case KKSwipeGestureDirectionLeft:
        // direction-specific code here
        break;
    case KKSwipeGestureDirectionUp:
        // direction-specific code here
        break;
    case KKSwipeGestureDirectionDown:
        // direction-specific code here
        break;
}

Solution

  • I think you're making a mistake, you're placing the code in a single method, but you should use two hands, one to determine the KKInput, and one to check the status, plus you forgot gestureSwipeEnabled

    try to do this:

    -(id) init {
    
        if ((self=[super init])) {
    
            input = [KKInput sharedInput];
            input.gestureSwipeEnabled = YES;
    
            [self schedule:@selector(theTime:)];
    
        }
        return self;
    }
    
    -(void) theTime:(ccTime)time {
    
        if (input.gestureSwipeRecognizedThisFrame) {
    
            KKSwipeGestureDirection dir = input.gestureSwipeDirection;
            switch (dir)
            {
                case KKSwipeGestureDirectionRight:
                    // direction-specific code here
                    break;
                case KKSwipeGestureDirectionLeft:
                    // direction-specific code here
                    break;
                case KKSwipeGestureDirectionUp:
                    break;
                case KKSwipeGestureDirectionDown:
                    // direction-specific code here
                    break;
                default:
                    break;
            }
        }
    }