Stephen Haney

Camera Shake with SpriteKit in Swift

Previously, we’ve covered using a master SKSpriteNode called ‘world’ to gather all of our game sprites on one parent sprite node. This allows us to move the entire ‘camera’ of our game by simply moving around this one sprite node. Now let’s apply a solid camera shake on important game events.

Swift Camera Shake Math

Camera shake is a super easy, amazingly fun feeling bit of juiciness you can add to your games. It will make events seem more important, and encourage repeated action from the game player. I’d highly encourage game devs to watch this YouTube video on the concept of juicy games if you haven’t already.

Here’s my World class with a basic swift camera shake function. Feel free to play with the intensity and time variables to create different types of shakes.

class World {
    let canvas = SKSpriteNode();
    var initialPosition:CGPoint?;

    func shakeCamera(duration:Float) {
        let amplitudeX:Float = 10;
        let amplitudeY:Float = 6;
        let numberOfShakes = duration / 0.04;
        var actionsArray:SKAction[] = [];
        for index in 1...Int(numberOfShakes) {
            // build a new random shake and add it to the list
            let moveX = Float(arc4random_uniform(UInt32(amplitudeX))) - amplitudeX / 2;
            let moveY = Float(arc4random_uniform(UInt32(amplitudeY))) - amplitudeY / 2;
            let shakeAction = SKAction.moveByX(moveX, y: moveY, duration: 0.02);
            shakeAction.timingMode = SKActionTimingMode.EaseOut;

        let actionSeq = SKAction.sequence(actionsArray);

Drop a comment if this helps you or if you have a different or better way of creating camera shake in iOS games.

Upgraded to play nice with outside SKActions and a few other improvements. Thanks to BenziAhamed and Daniel Griesser in the comments. 7/20/2014