gpt4 book ai didi

ios - 如果我将手指从我的节点移开并进入 GameView,为什么我的节点会停止旋转?

转载 作者:行者123 更新时间:2023-11-29 01:01:08 25 4
gpt4 key购买 nike

我有这个节点,如果我的手指在上面,我可以旋转它。我遇到的问题是,当我旋转节点时,我的手指从节点上滑落到 View 上,它停止旋转。如果我的手指不在节点上,我怎么还能进行触摸?

例如,我的应用程序中有一个 UISlider,如果我将手指放在 slider 上并向下移动,然后尝试左右移动 slider ,它仍然有效。我如何才能对触摸事件仍然存在的节点执行相同的操作?如果您不明白我在说什么,请告诉我。这是我用来旋转节点的代码:

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"
//lets user rotate when there is one finger on node.
let dy = circle.position.y - location.y
let dx = circle.position.x - location.x
let angle2 = atan2(dy, dx)
circle.zRotation = angle2
}

最佳答案

您应该将最后一个节点存储在一个实例变量中,然后使用它尝试保持旋转,即使它没有碰到它也是如此。

class MyClass  {
var lastNodeSelected:SKNode?

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

if node.name == "circle"
lastNodeSelected = node
}
}


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

if lastNodeSelected != nil {
let touchedNode = lastNodeSelected!
let dy = touchedNode.position.y - location.y
let dx = touchedNode.position.x - location.x
let angle2 = atan2(dy, dx)
touchedNode.zRotation = angle2
}

override func touchesEnd(touches: Set<UITouch>, withEvent event: UIEvent?) {
lastNodeSelected = nil

关于ios - 如果我将手指从我的节点移开并进入 GameView,为什么我的节点会停止旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969693/

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