Sprite Kit – 确定轻扫手势的向量来轻弹Sprite

我有一个游戏,从屏幕底部的圆形物体射击,我想能够刷他们轻扫我的方向滑动。 我的问题是,我不知道如何计算滑动的向量/方向,以便让圆形对象以适当的速度在适当的方向上弹起。

我使用的静态向量“(5,5)”需要通过滑动的滑动速度和方向来计算。 另外,我需要确保一旦我与对象第一次接触,就不会再发生,从而避免双击对象。 以下是我目前正在做的事情:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint location = [touch locationInNode:self]; SKNode* node = [self nodeAtPoint:location]; [node.physicsBody applyImpulse:CGVectorMake(5, 5) atPoint:location]; } } 

以下是如何检测滑动手势的示例:

首先,定义实例variables来存储起始位置和时间。

  CGPoint start; NSTimeInterval startTime; 

在touchesBegan中,保存触摸事件的位置/时间。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Avoid multi-touch gestures (optional) */ if ([touches count] > 1) { return; } UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; // Save start location and time start = location; startTime = touch.timestamp; } 

定义滑动手势的参数。 相应地调整这些。

 #define kMinDistance 25 #define kMinDuration 0.1 #define kMinSpeed 100 #define kMaxSpeed 500 

在touchesEnded中,通过比较开始和结束位置与时间戳之间的差异来确定用户的手势是否是轻扫。

 - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; // Determine distance from the starting point CGFloat dx = location.x - start.x; CGFloat dy = location.y - start.y; CGFloat magnitude = sqrt(dx*dx+dy*dy); if (magnitude >= kMinDistance) { // Determine time difference from start of the gesture CGFloat dt = touch.timestamp - startTime; if (dt > kMinDuration) { // Determine gesture speed in points/sec CGFloat speed = magnitude / dt; if (speed >= kMinSpeed && speed <= kMaxSpeed) { // Calculate normalized direction of the swipe dx = dx / magnitude; dy = dy / magnitude; NSLog(@"Swipe detected with speed = %g and direction (%g, %g)",speed, dx, dy); } } } } 

还有另一种方法可以做到这一点,你可以添加一个平移手势,然后从中获取速度:

首先在您的视图中添加平移手势:

 UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)]; [self.view addGestureRecognizer:gestureRecognizer]; 

然后处理这个手势:

 - (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { CGPoint location = [recognizer locationInView:recognizer.view]; if ([_object containsPoint:location]){ self.movingObject = YES; <.. object start moving ..> } } else if (recognizer.state == UIGestureRecognizerStateChanged) { if (!self.movingObject) return; CGPoint translation = [recognizer translationInView:recognizer.view]; object.position = CGPointMake(object.position.x + translation.x, object.position.y + translation.y); [recognizer setTranslation:CGPointZero inView:recognizer.view]; } else if (recognizer.state == UIGestureRecognizerStateEnded) { if (!self.movingObject) return; self.movingObject = NO; float force = 1.0f; CGPoint gestureVelocity = [recognizer velocityInView:recognizer.view]; CGVector impulse = CGVectorMake(gestureVelocity.x * force, gestureVelocity.y * force); <.. Move object with that impulse using an animation..> } } 

touchesBegan保存触摸位置作为一个CGPoint,你可以访问整个应用程序。

touchesEnded计算您的初始触摸(touchesBegan)和结束触摸(touchesEnded)的距离和方向。 然后应用适当的冲动。

为了避免双击,添加一个布尔canHit,当你使用冲动的时候你设置为NO,当你准备好再次点击的时候把它设置回YES。 在应用冲动之前,确保canHit设置为YES。