Scrolling in Coco2d

I'm trying to get swiping to work for the latest version of Cocos2d, here is my code:

-(void) setupGestureRecognizers { UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeLeft setNumberOfTouchesRequired:1]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft]; } 

He does not detect wipes at all!

UPDATE 1:

I updated the code to the next and still not found.

 -(void) setupGestureRecognizers { UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeLeft setNumberOfTouchesRequired:1]; [[[[CCDirector sharedDirector] openGLView] window] setUserInteractionEnabled:YES]; [[[CCDirector sharedDirector] openGLView] setUserInteractionEnabled:YES]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft]; } 
+6
source share
2 answers

I also tried to make this work, but I found a simpler and more efficient way to manage it.

so, for example, if you want to find napkins on the left, I would follow.

Declare two variables in the interface of your class

 CGPoint firstTouch; CGPoint lastTouch; 

In the init method of implementing your class, enable touch

 self.isTouchEnabled = YES; 

3. Add these methods to your class

 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; CGPoint location = [touch locationInView: [touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; //Swipe Detection Part 1 firstTouch = location; } -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; CGPoint location = [touch locationInView: [touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; //Swipe Detection Part 2 lastTouch = location; //Minimum length of the swipe float swipeLength = ccpDistance(firstTouch, lastTouch); //Check if the swipe is a left swipe and long enough if (firstTouch.x > lastTouch.x && swipeLength > 60) { [self doStuff]; } } 

the doStuff method is what is called if the left swipe occurred.

+11
source

The code is correct and should work.

You might want to make sure that neither the user interface nor touch input is disabled in the gl view or in the main window.

You should also check if there are any touches to cocos2d. The EAGLView class is the first receiver of strokes and sends them to CCTouchDispatcher. I can imagine that if you have target delegates, then they can β€œgrasp” the touches. Although cocos2d should only get strokes after gesture recognizers.

+3
source

Source: https://habr.com/ru/post/907485/


All Articles