SKAction.move(to: ...) 和 SKAction.move(by: ...) 对我来说完全一样

SKAction.move(to: ...) and SKAction.move(by: ...) does the exact same thing for me

我已经设置了一个 SKScene,我用一堆块填充它。触摸后,我想将一个随机块移动到 SKScene 的 0,0 位置。 0,0 不是最终目标,但我用它来测试。我的节点称为 bixel,我自然认为使用 SKAction.move(to: ...) 会将其移动到绝对坐标,如文档中所述,但是我必须遗漏一些东西,如移动到 0, 0 什么都不做,但是移动说 100,0 将它向右移动 100 像素。

我已经尝试了所有 move(to: ...) (moveTo(x: ...) 等。而且我已经尝试了 move(by: ...) ,它应该可以正常工作。 使用下面提供的代码,如果我在 action 和 action2 之间切换(通过重建项目),结果总是一样的,它向右移动了 100 像素。 我也试过点转换: let point = self.convert(CGPoint(x: 0, y: 0), to: bixel) 但是结果是一样的

let bixel = bixels[Int.random(in: 0..<bixels)] bixel.fillColor = .red

let action = SKAction.move(to: CGPoint(x: 100, y: 0), duration: 0.1)
let action2 = SKAction.move(by: CGVector(dx: 100, dy: 0), duration: 0.1)
bixel.zPosition = 1000
bixel.run(action)

编辑:你是完全正确的@Martin R。正如我在对你的回答的评论中所写,每个 ShapeNode 都认为他们的位置是 0,0。这是因为我如何初始化它们。 我这样做了:

let bixel = SKShapeNode(
    rect: CGRect(
        x: xOffset + (xF * shapeSize.width),
        y: self.size.height - yOffset - (yF * shapeSize.height),
        width: shapeSize.width - space,
        height: shapeSize.height - space
    ),
    cornerRadius: 2
)

这让它认为它的位置是 0,0。所以当我把它改成这个时:

let bixel = SKShapeNode(rectOf: shapeSize, cornerRadius: 2)
bixel.position = CGPoint(
    x: xOffset + (xF * shapeSize.width),
    y: self.size.height - yOffset - (yF * shapeSize.height)
)

一切正常。谢谢!

SKAction.move(to:duration:) 创建一个将节点移动到给定位置的动作。

SKAction.move(to: CGPoint(x: 100, y: 0), duration: 0.1)

创建一个将节点移动到位置 (100, 0) 的动作。

SKAction.move(by:duration:) 创建一个相对于当前位置移动节点的动作。

SKAction.move(by: CGVector(dx: 100, dy: 0), duration: 0.1)

创建一个动作,将节点从其当前位置 (x0, y0) 移动到新位置 (x0+100, y0)

当(且仅当)当前节点位置为 (0, 0).

时,这两个操作具有相同的效果