Gurmeet Khalsa
Gurmeet Khalsa

Reputation: 136

Spritekit: Animate node with running atlas and move node

I want animate a character as running using .atlas and simultaneously also move the character on screen. I was able to simulate running using atlas but don't know how to simultaneously move it. Right now I use this:

let animation = SKAction.animateWithTextures(frames, timePerFrame: 0.2)  

monsterNode.runAction((animation)).

Upvotes: 2

Views: 534

Answers (1)

Adam Eri
Adam Eri

Reputation: 965

You can, for instance, apply force to the node's physicsBody. This is great for a platformer, as obstacles and the landmark would slow down the character, making it more realistic.

let force = CGVector(dx: 9000, dy: 0)
self.physicsBody?.applyForce(force)

For a space racer or other more simple movement, you could simply apply velocity to it.

self.physicsBody?.velocity = CGVector(dx: 100, dy:10)

See SKPhysicsBody reference for more.

Update: For a very simple movement, you can use the SKAction's moveBy and moveTo methods and group the movement and the animation.

let vector = CGVector(dx:100, dy: 10)
let moveAction = SKAction.move(by: vector, duration: 1)
yourNode.run(SKAction.group([moveAction, animationAction])

Upvotes: 3

Related Questions