SKAction.waitForDuration在SpriteKit中不起作用

时间:2015-03-07 05:30:46

标签: ios swift sprite-kit

我一直在把这头看似简单的功能拉出来,我正在努力建立一个我在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)
}

1 个答案:

答案 0 :(得分:2)

waitForDuration SKAction可用于在行动序列中引入延迟,但它不会阻止调用runAction的线程 - 因此您的waitForDuration操作将设置为运行在您的节点上,然后将立即调用gameOverAction()

您可以在操作完成后使用runAction:completion的完成功能块执行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()
     })
}

根据createExplosion的内容,也可以将其合并到序列中,并且无需waitForDuration