gpt4 book ai didi

xcode - 即使我的手指在节点之外,如何让节点旋转?

转载 作者:行者123 更新时间:2023-11-30 10:04:52 24 4
gpt4 key购买 nike

我会尽力以最好的方式解释这一点。我有一个圆圈,当我的手指在圆圈上移动时,我可以旋转它。我遇到的问题是,当我旋转圆圈并将手指移到圆圈之外时,旋转就会停止。我希望即使我的手指在节点之外,它仍然具有触摸功能。当我在单个 View Controller 中时它可以工作,但在 spritekit 中我无法弄清楚。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)


if node.name == "circle" {

let dy = circle.position.y - location.y
let dx = circle.position.x - location.x
let angle2 = atan2(dy, dx)
circle.zRotation = angle2


}
}
}

最佳答案

声明一个可选的 SKNode 来保存当前旋转节点,如 varrotatingNode:SKNode?。您可以覆盖 touchesBegan

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
for touch in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)


if node.name == "circle" || node.name == "rightCircle" {

self.rotatingNode = node


}
}
}

从您的评论来看,您在 TouchMoved 中似乎还有其他代码,而不是您的问题中发布的代码。但如果你想旋转 Circle 或 rightCircle,那么可以这样说:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {




let dy = self.rotatingNode?.position.y - location.y
let dx = self.rotatingNode?.position.x - location.x
let angle2 = atan2(dy, dx)
self.rotatingNode?.zRotation = angle2



}
}

然后在touchesEnded中:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.rotatingNode = nil
}

关于xcode - 即使我的手指在节点之外,如何让节点旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590913/

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