2

Summary: I am trying to move a CCSprite "up" while the user is holding down on the screen. I am using Cocos2d v3.1 and working with Xcode 5.1.1

I believe I am supposed to use UILongPressGestureRecognizer to identify when the user is holding down on the screen. When I run the program I see the "Recognized Hold" message in the console. The CCAction is completed. After the Hold or LongPress is recognized, the sprite should move up until the user lifts their finger off the screen.

It is also my intention to use a Tap and Swipe gesture in the game. The gestures shouldn't be used simultaneously.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longHold:)];
longPress.minimumPressDuration=1.0f;
[[[CCDirector sharedDirector] view] addGestureRecognizer:longPress];

-(void) longHold:(UILongPressGestureRecognizer*)gesture{
    CCLOG(@"Recognized Hold");

    if(gesture.state==UIGestureRecognizerStateBegan){
        id climb = [CCActionMoveBy actionWithDuration:2.0f position:ccp(0,25)];

        [_hero runAction:climb];
        CCLOG(@"Recognized Hold Action");
    }
    else{
        return;
    }

}

My inclination is to use a while statement instead of an "if" statement in the longHold method. When I use the while statement the sprite doesn't move and when I lift my finger from the screen the while loop continues to execute until I stop the program.

This is what I see in the console:

2014-09-19 16:22:25.591 asdf [39402:60b] Recognized Hold
2014-09-19 16:22:25.665 asdf [39402:60b] Recognized Hold Action
2014-09-19 16:22:25.666 asdf [39402:60b] Recognized Hold Action
2014-09-19 16:22:25.666 asdf [39402:60b] Recognized Hold Action
2014-09-19 16:22:25.666 asdf [39402:60b] Recognized Hold Action 

Should I be using the following method:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
} 
Asdrubal
  • 2,421
  • 4
  • 29
  • 37

0 Answers0