2016-03-05 90 views
5

我在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 */ 
    } 
} 

回答

9

iPhone模擬器上的Xcode比在真實設備更大的CPU使用率。

在真實設備中測試以獲得有關CPU使用情況的真實指標。

+0

謝謝!在真正的手機上降至10%。 –

+1

@ATyshka如果這個答案解決了你的問題,你應該將它標記爲[accepted](http://stackoverflow.com/help/accepted-answer)。 – Whirlwind

+0

對不起!我是這個網站的新手 –

1

Tyshka,你需要重用資源。您的代碼爲完全相同的輸入圖像數據創建一個新的SKTexture。只需創建一個SKTexture,然後將其設置爲新節點的紋理。給你的代碼添加一個映射,以便SKTexture可以在給定紋理字符串名稱的情況下進行檢查和重用。

+0

是的,我意識到這一點。將解決! –