Adding animation to your toolkit
Before we dive back in to sprite theory, we should have some fun with our blue square. SpriteKit uses action objects to move sprites around the screen. Consider this example: if our goal is to move the square across the screen, we must first create a new action object to describe the animation. Then, we instruct our sprite node to execute the action. I will illustrate this concept with many examples in the chapter. For now, add this code in the didMove
function, below the self.addChild(mySprite)
line:
// Create a new constant for our action instance // Use the move action to provide a goal position for a node // SpriteKit will tween to the new position over the course of the // duration, in this case 5 seconds let demoAction = SKAction.move(to: CGPoint(x: 300, y: 150), duration: 3) // Tell our square node to execute the action! mySprite.run(demoAction)
Run the project. You will see our blue square slide across...