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.
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.
