Ios 如何让精灵在touchesEnd上移动?

Ios 如何让精灵在touchesEnd上移动?,ios,sprite,skaction,touchesmoved,touchesended,Ios,Sprite,Skaction,Touchesmoved,Touchesended,这是我用视频记录的问题 当tocuhesMoved drag line path完成后,当前面临一个bug,我希望精灵移动到触摸的最后一个位置。但是,您可以触摸UIView上的任何位置,它将移动到该位置,而无需使用触摸移动的拖曳线。如何仅使用拖曳线机制移动精灵,使精灵移动到该位置 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Moving"); if ([touches count]) {

这是我用视频记录的问题

当tocuhesMoved drag line path完成后,当前面临一个bug,我希望精灵移动到触摸的最后一个位置。但是,您可以触摸UIView上的任何位置,它将移动到该位置,而无需使用触摸移动的拖曳线。如何仅使用拖曳线机制移动精灵,使精灵移动到该位置

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

由于此条件,每次存在触摸事件时,都会执行touchesend中的所有内容

if ([touches count])
请注意,insidetouchesMoved为self.line2添加了一条路径,因此insidetouchesEnded可以执行以下操作

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


祝你好运

我已经有一段时间没有看到这一点了,但我相信你总是会在触摸时开始触摸和触摸,因此,在触摸移动中只需设置一个标志,在触摸移动中检查是否设置了标志,即触摸移动实际发生,如果是,移动“刀片”。一个更好的选择,因为你的路径似乎是一个属性,就是简单地检查touchesEnded中的CGPath以确定它是否为空?但对于您所说的备选方案,您可以使用(SKAction*)followPath:(CGPathRef)路径沿路径移动刀片,并将其设置为新位置,然后执行[self.line2 removeFromParent];不,我的意思是,正如Julio在下面添加的那样,测试CGPath是否包含用于回复的PointsHanks,但我仍然可以触摸并将精灵移动到某个位置,而无需执行拖曳线路径。我希望只有触摸精灵才能将其移动到新位置,而不是UIView上的任何其他位置。(给你发了一封电子邮件)如果(self.line2!=nil)你的精灵移动动作应该在里面,这样你的精灵只有在dragI认为我做了其他事情之后才会移动,如果(self.line2!=nil)我将源代码链接到你的话,它就不会跟着移动了。