gpt4 book ai didi

swift - 仅当 SKSpriteNode 类型为 Fruit(SKSpriteNode 的子类)时才用手指移动 SKSpriteNode

转载 作者:行者123 更新时间:2023-11-30 11:02:26 26 4
gpt4 key购买 nike

我正在创建一个游戏,用户可以在场景中移动一些水果。我希望用户只能移动场景中的水果,而不能移动场景中的任何其他 SKSpriteNode,因此我编写了下面的代码来实现它。然而,代码无法正常工作,因为我似乎无法拖动任何 Sprite ,而是只有当我停止触摸屏幕时它们才会改变位置,而且它们无论如何也不会移动太多。

    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。这样就可以将所有水果拖到我想要的任何地方。

关于swift - 仅当 SKSpriteNode 类型为 Fruit(SKSpriteNode 的子类)时才用手指移动 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53174979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com