【问题标题】:How to make a sprite move on touchesEnd?如何在 touchesEnd 上移动精灵?
【发布时间】:2015-08-10 05:28:27
【问题描述】:

这是我在视觉上记录的问题视频 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;
  }

}

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

【问题讨论】:

  • 自从我看这个已经有一段时间了,但我相信你总是在触摸时得到 touchesBegan 和 touchesEnded,所以,在你的 touchesMoved 中简单地设置一个标志,在 touchesEnd 检查是否设置了标志, ie touchesMoved 实际上发生了,如果是这样移动你的“刀片”。由于您的路径似乎是一个属性,因此更好的选择是简单地检查 touchesEnded 中的 CGPath 以确定它是否为空。
  • 我不太明白你说的标志是什么意思?但至于您所说的替代方案,您可以使用 (SKAction *)followPath:(CGPathRef)path 沿路径移动刀片并将其设置到新位置,然后执行 [self.line2 removeFromParent];
  • 不,我的意思是正如 Julio 在下面添加的那样,测试 CGPath 是否包含点

标签: ios sprite skaction touchesmoved touchesended


【解决方案1】:

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 内容只会在行路径存在时执行

祝你好运!!

【讨论】:

  • 感谢您的回复但是,我仍然可以触摸并将精灵移动到某个位置而无需执行拖动线路径。我想要唯一触摸精灵将其移动到新位置,而不是 UIView 上的任何其他位置。 (给你发了一封邮件)
  • 你的精灵移动动作应该在 if (self.line2 != nil) 这样你的精灵只会在拖动后移动
  • 如果 (self.line2 != nil) 我将源代码链接到您,我想我一定做了其他事情才不会遵循它。
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2015-05-20
相关资源
最近更新 更多