I have this class:
class Explosion : SKSpriteNode {
var gameScene : SKScene
var explosionPoint : CGPoint
init(_ gameScene : SKScene, _ explosionPoint : CGPoint){
self.explosionPoint = explosionPoint
self.gameScene = gameScene
let size : CGSize = CGSize(width: gameScene.size.width * CGFloat(0.23), height: gameScene.size.width * CGFloat(0.23))
super.init(texture: SKTexture(imageNamed: "Explosion_1"), color: UIColor.clear, size: size)
}
public func startExplosion(){
self.position = explosionPoint
gameScene.addChild(self)
let animation = SKAction.animate(withNormalTextures: getFrames(), timePerFrame: 0.1)
self.run(SKAction.sequence([animation, SKAction.removeFromParent()]))
}
private func getFrames() -> [SKTexture] {
var frames : [SKTexture] = []
for x in 1...12 {
frames.append(SKTexture(imageNamed: "Explosion_\(x)"))
let cat = SKTexture(imageNamed: "Explosion_\(x)")
print(cat)
}
return frames
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Now, this SKSpriteNode is supposed to be a gif, so I run the animation here:
public func startExplosion(){
self.position = explosionPoint
gameScene.addChild(self)
let animation = SKAction.animate(withNormalTextures: getFrames(), timePerFrame: 0.1)
self.run(SKAction.sequence([animation, SKAction.removeFromParent()]))
}
And I run it from the main SKScene like this:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
Explosion(self, touchLocation).startExplosion()
}
However, when the node is added to the scene, it doesn't actually run the animation. It stays with the initial texture. How do I make the node run the action?
I have tried assigning the explosion object to a variable and also tried using repeatForever, both failed. I also tried doing everything without using the custom class Explosion, like so:
var explosion : SKSpriteNode!
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
explosion = SKSpriteNode(imageNamed: "Explosion_1")
let lolsize : CGSize = CGSize(width: self.size.width * CGFloat(0.23), height: self.size.width * CGFloat(0.23))
explosion.position = touchLocation
explosion.size = lolsize
explosion.zPosition = 100
var textures = [SKTexture]()
let atlas = SKTextureAtlas(named: "Explosion")
for i in 1...atlas.textureNames.count {
let name = "Explosion_\(i)"
print(name)
textures.append(SKTexture(imageNamed: name))
}
self.addChild(explosion)
explosion.run(SKAction.repeatForever(SKAction.animate(withNormalTextures: textures, timePerFrame: 0.1)))
}
However, it gives the same result. The only texture that appears is the initial one "Explosion_1". There is no animation.