0

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

目前在 tocuhesMoved 拖动线路径完成后面临一个错误我希望精灵移动到最后一次触摸的位置。但是,您可以触摸 UIView 上的任何位置,它会移动到该位置,而无需使用 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;
  }

}

摸完~~~~~

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

[self.line2 removeFromParent];

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

1 回答 1

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,则仅在行路径存在时才会执行内容

祝你好运!!

于 2015-05-27T23:23:24.130 回答