精灵节点位置不随触摸更新



本质上,我想要的是当我触摸一个节点时,我希望能够在屏幕上移动它。问题是,每当我移动手指太快时,节点就会停止跟随它。

特别是我尝试这样做的spriteNodes具有物理实体和动画纹理,所以我尝试使用完全普通的spriteNode执行相同的代码,并且遇到了相同的问题。

这里的代码非常简单,所以我不确定这是否是我编写的内容的问题,或者它只是一个我无法修复的滞后问题。在整个触摸开始,触摸移动和触摸结束

也基本相同
for touch in touches {
  let pos = touch.location(in: self)
  let node = self.atPoint(pos)
  if node.name == "activeRedBomb"{
    node.position = pos
  }
  if node.name == "activeBlackBomb"{
    node.position = pos
  }

  if node.name == "test"{
    node.position.x = pos.x
    node.position.y = pos.y
  }

}

正在发生的事情是,如果你移动手指太快,那么在某些时候,触摸位置将不再在精灵上,所以你编码移动节点不会触发。

你需要做的是在touchesBegan()中设置一个标志来指示这个精灵被触摸,如果设置了标志,则将精灵移动到touchesMoved()中的触摸位置,然后在touchesEnded()中重置标志。

以下是您需要为此添加的大致内容:

import SpriteKit
class GameScene: SKScene {
var bombIsTouched = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if activeRedBomb.contains(touch.location(in: self)) {
            bombIsTouched = true
        }
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if bombIsTouched {
        activeRedBomb.position = (touches.first?.location(in: self))!
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if bombIsTouched {
        bombIsTouched = false
    }
}    

最新更新