SpriteKit fps在第一次动画调用时下降



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

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。

缓存中的问题在我这边,是:

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)
}

最新更新