I have been pulling my hair out over this seemingly simple feature I'm trying to build in to a game I am working on in SpriteKit and Swift. I have searched and can't figure why this isn't working.
What I want to do: When the player comes in contact with an enemy or an obstacle, I want to load and play through an explosion animation createExplosion() (The animation completes in 0.5 seconds) and then progress to the gameOverAction(). The problem is that only the first frame of the animation is shown and then it progresses to the gameOverAction(). I tried to make an SKAction.sequence but Xcode did not like me calling functions inside the sequence, so I found SKAction.waitForDuration. Problem is, it is having no effect on the execution of the code. How should this be done properly?
func collisionWithPlayer(playerObject: SKSpriteNode) {
runAction(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
createExplosion(playerObject)
playerObject.removeFromParent()
runAction(SKAction.waitForDuration(NSTimeInterval(0.5)))
gameOverAction()
}
And here is the code for the gameOverAction():
func gameOverAction() {
backgroundMusicPlayer.stop()
globalHighScore = NSUserDefaults.standardUserDefaults().objectForKey("GameHighScore") as? Int
localHighScore = self.monstersDestroyed
if globalHighScore == nil {
globalHighScore = localHighScore
}
globalHighScore = self.recordHighScore(localHighScore, highScore: globalHighScore!)
println(globalHighScore!)
let reveal = SKTransition.fadeWithDuration(1.0)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
}
func createExplosion(object: SKSpriteNode) -> SKSpriteNode {
var explosionArray = Array<SKTexture>()
var explosionSprite = SKSpriteNode()
explosionArray.append(SKTexture(imageNamed: "explosionSprite01.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite02.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite03.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite04.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite05.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite06.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite07.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite08.png"))
explosionArray.append(SKTexture(imageNamed: "explosionSprite09.png"))
let animateAction = SKAction.animateWithTextures(explosionArray, timePerFrame: 0.05)
explosionSprite = SKSpriteNode(texture:explosionArray[0])
explosionSprite.position = CGPoint(x: object.position.x + 10.0, y: object.position.y)
addChild(explosionSprite)
explosionSprite.runAction(SKAction.sequence([animateAction, SKAction.removeFromParent()]))
return(explosionSprite)
}