如何让一个精灵在touchesEnd上移动?

时间:2015-05-27 22:24:15

标签: ios sprite skaction touchesmoved touchesended

这是我用视觉记录问题的视频 https://www.youtube.com/watch?v=AC-4c4Qcyeo&feature=youtu.be

当tocuhesMoved拖动线路径完成后,当前面临一个错误我希望精灵移动到最后触摸位置的位置。但是你可以触摸UIView上的任何地方,它将移动到那个loaction而不使用touchesMoved拖动线。我如何仅使用拖动机制移动精灵来使精灵移动到该位置?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Moving");
if ([touches count]) {
UITouch* touch = [touches anyObject];
CGPoint position = [touch locationInNode:self];

path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, position.x, position.y);

CGPathAddLineToPoint(path, NULL, blade.position.x, blade.position.y);

//test points
//NSLog(@"%f", position.x);
//NSLog(@"%f", position.y);

self.line2.path = path;
  }

}

TouchesEnded ~~~~~

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

[self.line2 removeFromParent];

  if ([touches count]) {
     [blade runAction:[SKAction moveTo:[[touches anyObject]locationInNode:self]duration:0.21]];
  }
}

1 个答案:

答案 0 :(得分:0)

每次触摸事件都存在时, touchesEnded 内的所有内容都会被执行,这要归功于此条件

if ([touches count])

注意到 touchesMoved 内部添加 self.line2 的路径,因此在 touchesEnded 内可以执行以下操作

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  if (self.line2 != nil) {
    [self.line2 removeFromParent];

    if ([touches count]) {
      [blade runAction:[SKAction moveTo:[[touches anyObject]locationInNode:self]duration:0.21]];
    }
  }
}

因此,如果 self.line2 不等于 nil ,则仅当行路径存在时才会执行内容

祝你好运!!