SpriteKit中的CPU使用率非常高

时间:2016-03-05 19:58:11

标签: ios swift sprite-kit

我在SpriteKit中制作了一个简单的基于平铺的游戏,我在游戏中使用高CPU时遇到了一些困难。我有一个由60个图块组成的地图,每个图块是SKSpriteNode的子类。只需在场景中显示这60个精灵就可以在iPhone 6s模拟器中使用高达80%的CPU。没有动作,用户交互或物理进行。当我在UIKit而不是SpriteKit中制作相同的游戏时,我的CPU使用率为0.什么可以使用这么多的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")
    }

}

和我的Sprite Kit场景代码:

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

2 个答案:

答案 0 :(得分:10)

Xcode上的Iphone Simulator比实际设备具有更高的CPU使用率。

在真实设备中测试,以获得有关CPU使用率的真实指标。

答案 1 :(得分:1)

Tyshka,你需要重用资源。您的代码为完全相同的输入图像数据创建新的SKTexture。只需创建一个SKTexture,然后将其设置为新节点的纹理。在代码中添加一个贴图,以便在给定纹理字符串名称的情况下检查并重用SKTexture。