swift watchOS - SpriteKit应用程序由于CPU使用过多而被杀死

2skhul33  于 4个月前  发布在  Swift
关注(0)|答案(1)|浏览(69)

我的问题发生在watchOS上,但我猜这是一个一般的SpriteKit问题,所以.我有一个watchOS应用程序,它正在渲染SpriteKit场景(基本上是一个自定义MapView)。当我的用户在手表上平移时,我更新我的SpriteKit的位置,当用户转动表冠时,我更新它的比例。
所以现在当用户真的疯了,平移和缩放像疯了一样,我的场景将不得不经常更新,这将导致“崩溃”的原因,我超过了CPU的限制。

e.g.:
CPU: 9 seconds cpu time over 20 seconds (46% cpu average), exceeding limit of 15% cpu over 60 seconds

字符串
当sprit-nodes离开屏幕时,我已经缓存了它们(并在我可能再次需要它们时重用它们,而不是仅仅删除它们),这使得事情变得更好,但如果没有sprite被重用,CPU使用率又会上升。大部分时间都花在这里:

let texture = SKTexture(image: image)
let node = SKSpriteNode(texture: texture)


你会怎么处理这个问题?过一段时间后限制用户输入?或者有没有我没有发现的Info.plist检查告诉watchOS,如果我的应用使用更多的话,它是可以的?

liwlm1x9

liwlm1x91#

此指令

let texture = SKTexture(image: image)

字符串
之所以昂贵,是因为正如Apple文档所述:
图像数据在控制权返回游戏之前被复制。

TextureAtlas

相反,您应该以这种方式利用TextureAtlas

guard
    let enemyImage = UIImage(named: "panda.png"),
    let heroImage = UIImage(named: "hero.png")
else {
    fatalError()
}
let textureAtlas = SKTextureAtlas(dictionary: ["enemy": pandaImage,
                                               "hero": heroImage])


接下来,当你需要示例化一个Sprite使用一个特定的纹理,你写

guard let heroTexture = textureAtlas.textureNamed("hero") else { fatalError() }
let hero = SKSpriteNode(texture: heroTexture)

静态生成TextureAtlas

你也可以让Xcode按照这些说明为你填充TextureAtlas。

精灵图集

或者,您可以在these instructions之后切换到更现代的SpriteAtlas技术。

备注

当然,用更合适的错误管理来替换fatalError()

相关问题