Sebastian
Sebastian

Reputation: 600

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

I've set up an SKScene, that I fill with a bunch of blocks. Upon touch I want to move a random block to the 0,0 position of the SKScene. 0,0 is not the final goal, but I use it for test. My node is called a bixel and I naturally thought that using SKAction.move(to: ...) would move it to an absolute coordinate, as stated in the documentation, however I must be missing something, as moving to 0,0 does nothing, however moving to say 100,0 moves it 100 pixels to the right.

I've tried all the move(to: ...) (moveTo(x: ...) etc. And I've tried move(by: ...) which works as it should. With the code provided below, if I switch between action and action2 (by rebuilding the project), the outcome is always the same, it's moved 100 pixels to the right. I've also tried the point convert: let point = self.convert(CGPoint(x: 0, y: 0), to: bixel) but the result is the same.

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)

EDIT: You are completely correct @Martin R. And as I wrote in my comment to your answer every ShapeNode thought that their position was 0,0. It was because of how I initialised them. I did this:

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
)

Which made it think it's position was 0,0. So when I just changed it to this:

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

everything worked. Thanks!

Upvotes: 1

Views: 775

Answers (1)

Martin R
Martin R

Reputation: 539685

SKAction.move(to:duration:) creates an action that moves a node to the given position.

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

creates an action that moves a node to position (100, 0).

SKAction.move(by:duration:) creates an action that moves a node relative to its current position.

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

creates an action that moves a node from its current position (x0, y0) to the new position (x0+100, y0).

Both actions have the same effect if (and only if) the current node position is (0, 0).

Upvotes: 3

Related Questions