2013-01-18 43 views
1

我正试图实现游戏控制,这将改变移动方向的单元格 。所以,如果我做了正确的刷卡它转向右侧,如果我做下滑刷它转向下行方向等。在不中断触摸的情况下检测2次刷卡

这是cocos2d游戏,我使用CCNode+SFGestureRecognizersUISwipeGestureRecognizer

在现在我有未来实现它

UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)]; 
     [self addGestureRecognizer:rightSwipeGestureRecognizer]; 
     rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; 
     rightSwipeGestureRecognizer.delegate = self; 
     [rightSwipeGestureRecognizer release]; 

     UISwipeGestureRecognizer *upSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleUpSwipe:)]; 
     [self addGestureRecognizer:upSwipeGestureRecognizer]; 
     upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp; 
     upSwipeGestureRecognizer.delegate = self; 
     [upSwipeGestureRecognizer release]; 

     UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)]; 
     [self addGestureRecognizer:leftSwipeGestureRecognizer]; 
     leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; 
     leftSwipeGestureRecognizer.delegate = self; 
     [leftSwipeGestureRecognizer release]; 

     UISwipeGestureRecognizer *downSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleDownSwipe:)]; 
     [self addGestureRecognizer:downSwipeGestureRecognizer]; 
     downSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown; 
     downSwipeGestureRecognizer.delegate = self; 
     [downSwipeGestureRecognizer release]; 

但问题是,认识到以下的姿态,你需要从屏幕上抬起手指。如果您在第一次滑动之后不抬起手指,则只会识别第一次滑动。

在现在:

enter image description here

如何应该是:

enter image description here

什么是最好的方法呢?谢谢!

+0

我不熟悉cocos2d的,但听起来好像你会更好,自己跟踪接触和通过跟踪先前的x的确定的方向, y和当前的x,y。轻扫手势通常是一个固定的循环手势(您只需执行一次),并且不会触发连续运动(否则所有操作都将是轻扫)。在标准的UIKit中,你有touchesMoved:当触摸在屏幕上移动时被调用的方法。不知道cocos2d是什么,但它可能值得研究。 – nebs

回答

1

好的,有我的解决方案。我不认为这真的是一个好的解决方案,但在我的情况下,它是有效的,因为我需要在每个时间间隔获取方向。

所以,在我CCLayer子类:

#define SWIPE_LENGTH  60 
#define SWIPE_TANGENT  2 

... 

@property(nonatomic, assign) CGPoint latestLocation; 

... 

-(id) init 
{ 
    if((self=[super init])) 
     { 
      self.isTouchEnabled = YES; 
     } 
    return self; 
} 

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    self.latestLocation = location; 
} 

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    CGPoint delataLocation = CGPointMake(location.x - self.latestLocation.x, location.y - self.latestLocation.y); 
    if (sqrt(pow((delataLocation.x), 2) + pow((delataLocation.y), 2)) > SWIPE_LENGTH) { 
     if (delataLocation.y > 0 && delataLocation.y > SWIPE_TANGENT * ABS(delataLocation.x)) 
     { 
      [self handleSwipeWithDirestion:SDirectionTypeUp]; 
     } else if (delataLocation.y < 0 && ABS(delataLocation.y) > SWIPE_TANGENT * ABS(delataLocation.x)) 
     { 
      [self handleSwipeWithDirestion:SDirectionTypeDown]; 
     } else if (delataLocation.x > 0 && delataLocation.x > SWIPE_TANGENT * ABS(delataLocation.y)) 
     { 
      [self handleSwipeWithDirestion:SDirectionTypeRight]; 
     } else if (delataLocation.x < 0 && ABS(delataLocation.x) > SWIPE_TANGENT * ABS(delataLocation.y)) 
     { 
      [self handleSwipeWithDirestion:SDirectionTypeLeft]; 
     } 
     self.latestLocation = location; 
    } 
} 
1

cancelsTouchesInView应该有帮助,它可以防止手势识别器识别手势时取消触摸事件。这将允许其他手势识别器继续检查他们的手势。

rightSwipeGestureRecognizer.cancelsTouchesInView = NO; 
    upSwipeGestureRecognizer.cancelsTouchesInView = NO; 
    leftSwipeGestureRecognizer.cancelsTouchesInView = NO; 
    downSwipeGestureRecognizer.cancelsTouchesInView = NO; 
+0

谢谢,但这种解决方案不适用于我的情况。可能因为CCNode + SFGestureRecognizers.h的实现而无法工作。我会探讨这一点 – BergP