I have the function that moves an object and runs animation on move:
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)
}
Also I have the cacher:
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
}
So, the problem is that during first call fps drops significantly and CPU time increases, for example: move object to the left - from 30 fps it drops to 8 fps.