SpriteKit Battery/Energy Drain

Viewed 400

I have been developing a game for a little while now and have come to realize that my app quickly drains the device battery. I have measured the energy drain through XCode instruments and it is generally in the 17/20 range. I have a handful of background animations and disabling those does seem to help. Out of curiosity, I started a project with the following simple code:

import SpriteKit
import GameplayKit

class GameScene: SKScene {

let NumberOfSprites = 1


override  func didMove(to view: SKView)
{
    view.showsDrawCount = true
    view.showsFPS = true
    view.showsNodeCount = true

    // Set scene to 414x736 with anchorPoint in the bottom left corner
    self.anchorPoint = CGPoint(x: 0, y: 0)
    self.size = CGSize(width: 414, height: 736)

    var spriteArray = [SKSpriteNode]()

    for _ in 0..<NumberOfSprites
    {
        let sprite = SKSpriteNode(color: .black, size: CGSize(width: 50, height: 50))
        sprite.position = CGPoint(x: Int(arc4random_uniform(UInt32(414))), y: Int(arc4random_uniform(UInt32(736))))
        self.addChild(sprite)
        spriteArray.append(sprite)
    }


    let moveAction = SKAction.move(by: CGVector(dx: 50, dy: 50), duration: 1.0)
    let moveAction2 = SKAction.move(by: CGVector(dx: -50, dy: -50), duration: 1.0)

    let repeatAction = SKAction.repeatForever(SKAction.sequence([moveAction, moveAction2]))

    for sprite in spriteArray
    {
        sprite.run(repeatAction)
    }

}


}

This code will put one black box on the screen and move it x+50, y+50, then x-50, y-50 indefinitely. Below is the energy impact of doing this. I measured an Energy Usage Level on the device around 7-8/20 on average.

enter image description here

I was able to add up to 200,000 nodes (just change the NumberOfSprites variable) and the energy impact increased to 18/20. That of course makes sense to me, though I find it interesting that my app which has maybe 120 nodes on the screen at a given time, with only a handle full of actions, is generating almost as much energy drain.

My question is: If one simple action causes such an energy impact, how are developers handling this in their games? There are many complex games in the app store that don't seem to drain battery nearly this quickly. Am I missing something?

Thanks in advance for any help.

1 Answers

High CPU consumption is often a draw count issue. You can reduce your draw count by utilizing sprite atlases, though these have size limits and other quirks worth reviewing in the Swift documentation. It is particularly worth noting that when you load many sprites into an atlas, when that atlas is accessed it places all those objects into memory, so you should be mindful of memory usage here as you seek to reduce your CPU load. I would also add that most games with substantial animation have very high energy usage, so there is a limit to "what you can do."

Related