cocos2d-iphone – 在Coco2d中滑动

前端之家收集整理的这篇文章主要介绍了cocos2d-iphone – 在Coco2d中滑动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让刷卡工作为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];


}
我也尝试过这项工作,但我发现了一种更简单,也更好的控制方法.

所以例如,如果你想要检测左边的滑动我会这样跟随.

在你的类的接口中声明两个变量

CGPoint firstTouch;
CGPoint lastTouch;

在init方法中执行你的类启用touches

self.isTouchEnabled = YES;

3.将这些方法添加到您的班级

-(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];
    }

}

如果发生左滑动,则调用方法“doStuff”.

原文链接:https://www.f2er.com/cocos2dx/338115.html

猜你在找的Cocos2d-x相关文章