1

我正在尝试实现将改变单位移动方向的游戏控制。因此,如果我向右滑动它会向右转,如果我向下滑动它会转向向下等等。

这是 cocos2d 游戏,我正在CCNode+SFGestureRecognizers使用UISwipeGestureRecognizer.

现在我有下一个实现它

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

但问题是要识别以下手势,您需要将手指从屏幕上抬起。如果您在第一次滑动后没有抬起手指,它只会识别第一次滑动。

现在:

在此处输入图像描述

应该如何:

在此处输入图像描述

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

4

2 回答 2

1

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

  rightSwipeGestureRecognizer.cancelsTouchesInView = NO;
  upSwipeGestureRecognizer.cancelsTouchesInView = NO;
  leftSwipeGestureRecognizer.cancelsTouchesInView = NO;
  downSwipeGestureRecognizer.cancelsTouchesInView = NO;
于 2013-01-18T14:46:00.533 回答
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;
    }
}
于 2013-01-20T20:20:24.307 回答