Quicksillver
Quicksillver

Reputation: 307

Cannot get SKActionmove to work reliably

I'm trying to build a small spriteKitGame. I have been trying to use the function to move a couple of tank sprite nodes to a specific point.

Attached below is the code snippet.

let tankSpawn = CGPoint(x: self.size.width , y: 70);
        tank.position = tankSpawn;
        tank.zPosition = 3.0;

        let targetPoint = CGPoint(x: -tank.size.width/2, y: tank.position.y);

        let actionMove = SKAction.move(to: targetPoint, duration: TimeInterval(tankMoveDuration))

This is my result. They are spawning at the correct point ( 70units high ), but are going down as shown.

enter image description here

I want them to go in a straight line. I set the target points y as a constant. I have no idea why they are going to wards some bottom source.

I have similar code for planes that spawn above( which are working perfectly ).

  let plane = SKSpriteNode(imageNamed: "SpaceShip");

        let planeMoveDuration = 3.0
        let planeSpawn = CGPoint(x: self.size.width , y: self.size.height/2);
        plane.position = planeSpawn;
        plane.zPosition = 3.0;

        let actionMove = SKAction.move(to: CGPoint(x: -plane.size.width/2, y: plane.position.y), duration: TimeInterval(planeMoveDuration))

I have no idea what my mistake here is.

I have tried changing the y co ordinate of the target to tank.position.y, but it doesn't work.

Upvotes: 0

Views: 40

Answers (1)

Steve Ives
Steve Ives

Reputation: 8134

Are they falling under gravity? A sprite-kit scene with physics bodies will have gravity and things will fall unless you take specific action to avoid this.

It's easily done - you're developing your game, you have basic elements on screen and movement etc is all working well, then you want to add some collision detection so you add physicsBodies and then whoosh - where'd my sprites go?

Upvotes: 3

Related Questions