【问题标题】:SpriteKit fps drops at first animation callSpriteKit fps 在第一次动画调用时下降
【发布时间】:2016-04-19 10:02:20
【问题描述】:

我有移动对象并在移动时运行动画的功能:

func animateMove(move: MoveTo, completion: () -> ()) {
    let object = move.object
    let spriteName = "\(object.spriteName)\(move.direction.name)"

    let textures = TextureCache.loadTextures(spriteName)
    let animate = SKAction.animateWithTextures(textures, timePerFrame: moveDuration/NSTimeInterval(textures.count))

    let point = pointForColumn(move.column, row: move.row)
    let move = SKAction.moveTo(point, duration: moveDuration)
    move.timingMode = .Linear

    let group = SKAction.group([move, animate])
    object.sprite!.removeAllActions()
    object.sprite!.runAction(group, completion: completion)
}

我还有缓存:

class TextureCache {
...

static func loadTextures(name: String) -> [SKTexture] {
    let atlas = "\(name).atlas"
    return TextureCache.sharedInstance.loadTexturesFromAtlas(atlas, name: name)
}

private func loadTexturesFromAtlas(atlas: String, name: String) -> [SKTexture] {
    if let textures = textureDictionary["\(atlas):\(name)"] {
        return textures
    }

    let textureAtlas = SKTextureAtlas(named: atlas)
    var textures = [SKTexture]()
    for i in 0..<textureAtlas.textureNames.count {
        textures.append(SKTexture(imageNamed: "\(name)\(i)"))
    }

    textureDictionary["\(atlas):\(name)"] = textures

    return textures
}

因此,问题在于在第一次调用期间 fps 显着下降并且 CPU 时间增加,例如:将对象向左移动 - 从 30 fps 下降到 8 fps。

【问题讨论】:

  • 你在哪里初始化预加载纹理?
  • 在从 SKScene 类派生的“init”方法中。基本上,animateMove 方法是该类的成员。
  • 你真的试过这个吗:preloadTextures: withCompletionHandler
  • 由于某种原因,当我使用 preloadTextures: withCompletionHandler 程序崩溃时,需要找出原因。但我尝试了另一种方法,在主题中提到,为每个纹理调用 size 方法。问题依然存在。
  • 看看这个例子,然后尝试一下:stackoverflow.com/a/29421625/3402095 那是一种方式(UI 元素应该在主线程上更新)...另一种是使用size 属性,但是你说那个方法也没用。就个人而言,我无法复制您所说的内容,因此我无法对此发表更多评论。此外,我不知道您当前的设置,因此错误可能在您身边。作为最后的手段,尝试在一个空项目中重现您所说的内容。只需使用所需的纹理/图集,看看你是否仍然遇到崩溃。如果没有,请仔细检查您的代码逻辑。

标签: ios sprite-kit swift2 skspritenode


【解决方案1】:

问题出在缓存器中,是:

let textureAtlas = SKTextureAtlas(named: atlas)
var textures = [SKTexture]()
for i in 0..<textureAtlas.textureNames.count {
    textures.append(SKTexture(imageNamed: "\(name)\(i)"))
}

现在:

let textureAtlas = SKTextureAtlas(named: atlas)
var textures = [SKTexture]()
for i in 0..<textureAtlas.textureNames.count {
    let texture = textureAtlas.textureNamed("\(name)\(i)")
    texture.size()
    textures.append(texture)
}

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多