我试图让刷卡工作为Cocos2d最新版本这里是我的代码:
-(void) setupGestureRecognizers { UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeLeft setNumberOfTouchesrequired:1]; [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft]; }
它完全没有检测到滑动!
更新1:
-(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]; }
我也尝试过这项工作,但我发现了一种更简单,也更好的控制方法.
原文链接:https://www.f2er.com/cocos2dx/338115.html所以例如,如果你想要检测左边的滑动我会这样跟随.
在你的类的接口中声明两个变量
CGPoint firstTouch; CGPoint lastTouch;
在init方法中执行你的类启用touches
self.isTouchEnabled = YES;
-(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]; } }