Illep Illep - 11 months ago 77
Swift Question

SKSpriteNode animation to appear bottom-up

According to the following code, i am displaying a mountain (hill) in the scene at x =

and y =
self.frame.height / 2

However, I want the hill to appear from bottom-up (from below the scene) like an animation. How can I code this ?

let hill = SKSpriteNode(imageNamed: "hill")

hill.position = CGPoint(x: 400, y: self.frame.height / 2 )


hill.physicsBody = SKPhysicsBody(rectangleOfSize: hill.size)

hill.physicsBody?.categoryBitMask = 1

hill.physicsBody?.collisionBitMask = 2


Nik Nik
Answer Source

To create an animation that makes the sprite slide up from "under" the scene, do this:

Set its initial position to below the scene:

hill.position = CGPoint(x: 400, y: self.frame.height / CGFloat(2) - self.frame.height)

Note: - self.frame.height is used to place the node 1 full frame height below the desired location

Then start an action that moves it up to its desired position in didMoveToView, or wherever you want the animation to start:

let moveUpAnimation = SKAction.move(to: CGPoint(x: 400, y: self.frame.height / CGFloat(2)), duration: 0.75)

This example creates an action that takes 0.75 seconds to move the sprite up one full frame height. You can set its initial position to anywhere below the visible scene to create this effect, and change the duration to any desired speed.