Very high CPU utilization in SpriteKit

I am making a simple tile based game in SpriteKit and I am having difficulty with high CPU usage in my game. I have a map of 60 tiles, and each tile is a subclass of SKSpriteNode. Simple display of these 60 sprites on stage uses up to 80% of the processor in the iPhone 6s simulator. No movement, user interaction or physics. When I made the same game in UIKit and not SpriteKit, my processor was 0. What could be so much processor?

my tile class:

import SpriteKit import UIKit class Tile: SKSpriteNode { var tileType = "grass", tileX = 0, tileY = 0 init (tileType: String, tileX: Int, tileY: Int) { self.tileType = tileType self.tileX = tileX self.tileY = tileY let texture = SKTexture(imageNamed: tileType) super.init(texture: texture, color: UIColor(), size: texture.size()) self.userInteractionEnabled = true self.position = CGPoint(x: CGFloat(45+64*(tileX-1)), y: CGFloat(47+56*(tileY-1))) self.zPosition = -1 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 

and Sprite Kit script code:

 import SpriteKit var map: [[String]] = [["grass","water","grass","rocky","rocky","grass","grass","grass","grass","water"],["grass","water","grass","grass","rocky","rocky","grass","grass","water","water"],["grass","water","water","grass","rocky","grass","grass","water","water","water"],["grass","grass","water","rocky","rocky","grass","grass","water","water","water"],["grass","grass","water","rocky","rocky","grass","water","water","water","water"],["grass","grass","water","rocky","rocky","grass","water","water","water","water"] ] class GameScene: SKScene { override func didMoveToView(view: SKView) { /* Setup your scene here */ for (rowNumber, row) in map.enumerate() { for (columnNumber, type) in row.enumerate() { let theTile = Tile(tileType: type, tileX: columnNumber+1, tileY: rowNumber+1) self.addChild(theTile) } } self.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0) } override func update(currentTime: CFTimeInterval) { /* Called before each frame is rendered */ } } 
+5
source share
2 answers

The iPhone Simulator on Xcode has much more CPU utilization than a real device.

Test the real device to have a real CPU utilization.

+9
source

Dad, you need to reuse resources. Your code creates a new SKTexture for the same image input. Just create one SKTexture and then set it as a texture for the new node. Add a map to your code so that SKTexture can be checked and reused based on the name of the texture string.

+1
source

Source: https://habr.com/ru/post/1244498/


All Articles