我在SpriteKit中製作了一個簡單的基於磁貼的遊戲,並且在我的遊戲中CPU使用率很高。我有一張由60個貼圖組成的地圖,每個貼圖都是SKSpriteNode的子類。只是在場景中顯示這60個精靈在iPhone 6s模擬器中使用多達80%的CPU。沒有運動,用戶交互或物理進行。當我在UIKit中製作相同的遊戲而不是SpriteKit時,我的CPU使用率爲0.什麼可以使用這麼多的CPU?SpriteKit中的CPU使用率非常高
我的瓷磚類:
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")
}
}
和我的雪碧套件現場代碼:
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 */
}
}
謝謝!在真正的手機上降至10%。 –
@ATyshka如果這個答案解決了你的問題,你應該將它標記爲[accepted](http://stackoverflow.com/help/accepted-answer)。 – Whirlwind
對不起!我是這個網站的新手 –