我一直在为这个看似简单的功能而发愁,我正试图在SpriteKit和Swift中构建一个游戏。我已经找过了,不明白为什么这不管用。
我想做的是:
当玩家接触到敌人或障碍物时,我想加载并播放一个爆炸动画createExplosion()(动画在0.5秒内完成),然后进入gameOverAction()。问题是,只显示动画的第一帧,然后进入gameOverAction()。
我试图创建一个SKAction.sequence,但是Xcode不喜欢我在序列中调用函数,所以我找到了SKAction.waitForDuration。问题是,它对代码的执行没有影响。
怎么做才是正确的?
func collisionWithPlayer(playerObject: SKSpriteNode) {
runAction(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
createExplosion(playerObject)
playerObject.removeFromParent()
runAction(SKAction.waitForDuration(NSTimeInterval(0.5)))
gameOverAction()
}
下面是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)
}
最佳答案
SKAction可以用来在操作序列中引入延迟,但它不会阻止线程调用,因此您的waitForDuration
操作将设置为在节点上运行,但随后将立即调用runAction
。
您可以使用waitForDuration
的完成块在操作完成后执行gameOverAction()
-
func collisionWithPlayer(playerObject: SKSpriteNode) {
var actions = Array<SKAction>();
actions.append(SKAction.playSoundFileNamed("spaceExplosion.mp3", waitForCompletion: false))
actions.append(SKAction.waitForDuration(NSTimeInterval(0.5)))
actions.append(SKAction.removeFromParent())
createExplosion(playerObject)
let sequence = SKAction.sequence(actions);
playerObject.runAction(sequence,completion: { () -> Void in
self.gameOverAction()
})
}
根据
runAction:completion
的内容,也可以将其合并到序列中,并消除对gameOverAction()
的需要关于ios - SKAction.waitForDuration在SpriteKit中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28911737/