Pablo Garcia

Apps for iPhone, iPad, Apple TV and Apple Watch

Spritekit - Background Scrolling - III

Jul 31, 2019

In this final post we will add speed to the background movement. When we will press the screen, we will increase the speed of the background, and if we stop pressing the screen, the speed will decrease to zero.

This will be the result:

Gesture recognizer

We need to know when the user is pressing the screen, so in the didMove function, we have added a UILongPressGestureRecognizer var and added to the scene's view.

override func didMove(to view: SKView) {

        let longPressGR = UILongPressGestureRecognizer(target: self, action: #selector(longPress(sender:)))
        longPressGR.delegate = self
        longPressGR.minimumPressDuration = 0.1
        self.view?.addGestureRecognizer(longPressGR)

        speedLabel = SKLabelNode(fontNamed: "Chalkduster")
        speedLabel.fontSize = 50
        speedLabel.text = "0.0"
        speedLabel.horizontalAlignmentMode = .right
        speedLabel.position = CGPoint(x: 400, y: 700)
        addChild(speedLabel)
    }

update function

We have changed the update function of the BackgroundScroll class, adding a new boolean parameter to tell the backgroundScroll if he has to increase (or not) the background speed.



public func update(deltaTime: TimeInterval, increaseVelocity: Bool)

And that's all folks!!

You have the code here. Hope you enjoy it!