问题
I want to make a animation to my game when two sprite nodes have a collision. So i create this function :
func contactEntreMeteorites(Meteorites : SKSpriteNode , Meteorites2 : SKSpriteNode){
Meteorites.removeFromParent()
Meteorites2.removeFromParent()
let ExplosionTexture1 = SKTexture(imageNamed: "Explosion1.png")
let ExplosionTexture2 = SKTexture(imageNamed: "Explosion2.png")
let ExplosionTexture3 = SKTexture(imageNamed: "Explosion3.png")
let ExplosionTexture4 = SKTexture(imageNamed: "Explosion4.png")
let ExplosionTexture5 = SKTexture(imageNamed: "Explosion5.png")
let ExplosionTexture6 = SKTexture(imageNamed: "Explosion6.png")
let ExplosionTexture7 = SKTexture(imageNamed: "Explosion7.png")
let ExplosionTexture8 = SKTexture(imageNamed: "Explosion8.png")
let ExplosionTexture9 = SKTexture(imageNamed: "Explosion9.png")
let ExplosionTexture10 = SKTexture(imageNamed: "Explosion10.png")
let ExplosionTexture11 = SKTexture(imageNamed: "Explosion11.png")
let ExplosionTexture12 = SKTexture(imageNamed: "Explosion12.png")
let animateExplosion = SKAction.sequence([
SKAction.waitForDuration(0, withRange: 0),
SKAction.animateWithTextures([ExplosionTexture1,ExplosionTexture2,ExplosionTexture3,ExplosionTexture4,ExplosionTexture5,ExplosionTexture6,ExplosionTexture7,ExplosionTexture8,ExplosionTexture9,ExplosionTexture10,ExplosionTexture11,ExplosionTexture12 ], timePerFrame: 0.1)
])
Explosion = SKSpriteNode(texture: ExplosionTexture1)
Explosion.position = CGPointMake(Meteorites.position.x, Meteorites.position.y)
Explosion.runAction(animateExplosion)
self.addChild(Explosion)
}
This code works perfectly but I don't know how can I delete the SpriteNode
"Explosion" when my SKAction sequence
animateExplosion is finished.
Thanks for helping :) And sorry for my english i'm french ;)
回答1:
You've implemented the wrong method. Instead of runAction:
, implement runAction:completion:
. The completion
method will be called on completion! So that is the place to remove the explosion node.
回答2:
So this is the solution :
Explosion = SKSpriteNode(texture: ExplosionTexture1)
Explosion.position = CGPointMake(Meteorites.position.x, Meteorites.position.y)
self.addChild(Explosion)
Explosion.runAction(animateExplosion, completion : {self.Explosion.removeFromParent()})
来源:https://stackoverflow.com/questions/33266503/make-explosion-animation-skaction-and-remove