节点移动后SKSpriteNode的X、Y坐标不变

X, Y coordinates of SKSpriteNode do not change after the node has been moved

我有以下代码可以将 SKSpriteNode 从屏幕外的位置移动到屏幕上的位置。 spawnNode 创建起始位置在屏幕外的节点。 nodePositions 是一个结构数组,包含屏幕上的 x、y 和 Zposition 点。代码确实将节点移动到我想要的位置。但是,我想使用当前节点位置稍后定位更多节点。打印语句显示相同的结果。 Pre & Post 是一样的。这对我来说毫无意义。谁能给我解释一下?看起来可能必须创建连续节点的子节点。我不想这样做。

    spawnNode()
    allNodes[0].texture = SKTexture(imageNamed: imageName)

    print("Pre: x: \(String(describing: allNodes[0].position.x))")
    print("Pre: y: \(String(describing: allNodes[0].position.y))")

    let duration: CGFloat = 0.5
    moveNodePos = SKAction.moveBy(x: nodePositions[0].posX, y: nodePositions[0].posY, duration: TimeInterval(duration))
    moveNodePos.timingMode = .easeIn
    allNodes[0].zPosition = nodePositions[0].zPosition
    allNodes[0].run(moveNodePos)
    print("Post: x: \(String(describing: allNodes[0].position.x))")
    print("Post: y: \(String(describing: allNodes[0].position.y))")

预:x:0.0 预:y:500.0 Post: x: 0.0 Post: y: 500.0

SKAction 随着时间的推移发生,因为程序反复通过 the rendering loop

由于程序没有在您的 allNodes[0].run(moveNodePos) 运行 语句和随后的 print 语句之间通过渲染循环,因此该操作没有机会修改打印语句时节点的位置。

SKAction 在 0.5 秒的持续时间内移动节点。 allNodes[0].run(moveNodePos) 行被执行,moveBy 动作开始,其后的代码(即打印语句)立即被执行。这意味着当您执行打印语句时,操作仍未完成并且节点尚未移动。尝试在操作完成后打印节点的位置,例如使用

allNodes[0].run(moveNodePos, completion: { insert the print statements here } )

希望对您有所帮助!