gpt4 book ai didi

swift - SKScene + TouchesBegan Bug

转载 作者:行者123 更新时间:2023-11-30 10:12:25 27 4
gpt4 key购买 nike

我的 SKScene 有 1 个空节点 (buttonsGroup)。在我的 GameView 类中,我创建一个节点 (ball1),它是空节点的父节点。

    buttonsGroup = childNodeWithName("ball1")


var ball1 = SKSpriteNode(imageNamed: "ball")
ball1.name == "ball1"
buttonsGroup.addChild(ball1)

当我尝试使用 TouchesBegan 移动节点时出现问题:

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

for touch in (touches as! Set<UITouch>) {

let location = touch.locationInNode(self)
let touchNode = nodeAtPoint(location)

if touchNode.name != "ground" {
touchNode.position = location
}
}
}

我已经知道,如果您使用 SKScene 和 touchNode 位置,它们将返回不同的值(或者至少看起来如此)。在这种情况下,节点只是跳转到任何地方。我的 SKScene 的大小是 480x320。

有人知道解决办法吗?如果我手动执行(没有 SKScene),它可以正常工作。在尝试使用 moveBy 操作或任何其他需要我在 View 中的位置的操作时,我已经多次遇到此问题。

最佳答案

如果您尝试移动球,则需要使用 convertPoint:toNode 将触摸位置从场景坐标转换为球的父坐标。或convertPoint:fromNode .

例如,

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if (node.name != "ground") {
let locationInNode = convertPoint(location, toNode:node.parent!)
node.position = locationInNode
}
}
}

关于swift - SKScene + TouchesBegan Bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32235959/

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