仅当 SKSpriteNode 类型为 Fruit(SKSpriteNode 的子类)时才用手指移动 SKSpriteNode
Moving SKSpriteNode with finger only if it is of type Fruit (subclass of SKSpriteNode)
我正在创建一个用户可以在场景中移动一些水果的游戏。我希望用户只能移动水果而不是场景中的任何其他 SKSpriteNode,所以我编写了下面的代码来实现它。但是代码无法正常工作,因为我似乎无法拖动我的任何精灵,而是只有当我停止触摸屏幕时它们才会改变位置并且它们不会移动太多。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesTouched = nodes(at: location)
for node in (nodesTouched) {
if node is Fruit{
for t in touches {
let locationMoved = t.location(in: self)
node.position.x = locationMoved.x
node.position.y = locationMoved.y
}
}
}
}
}
有人知道它有什么问题吗?
提前致谢!
我找到了一个解决方案,基本上是在每次触摸该特定 Fruit 实例时将 physicsBody.affectedByGravity 属性 设置为 false,然后尽快将其设置回 true当我停止触摸它时。这使得我可以将所有水果拖到我想要的任何地方。
我正在创建一个用户可以在场景中移动一些水果的游戏。我希望用户只能移动水果而不是场景中的任何其他 SKSpriteNode,所以我编写了下面的代码来实现它。但是代码无法正常工作,因为我似乎无法拖动我的任何精灵,而是只有当我停止触摸屏幕时它们才会改变位置并且它们不会移动太多。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let location = touch?.location(in: self){
let nodesTouched = nodes(at: location)
for node in (nodesTouched) {
if node is Fruit{
for t in touches {
let locationMoved = t.location(in: self)
node.position.x = locationMoved.x
node.position.y = locationMoved.y
}
}
}
}
}
有人知道它有什么问题吗?
提前致谢!
我找到了一个解决方案,基本上是在每次触摸该特定 Fruit 实例时将 physicsBody.affectedByGravity 属性 设置为 false,然后尽快将其设置回 true当我停止触摸它时。这使得我可以将所有水果拖到我想要的任何地方。