swift spritekit如何运行具有指定持续时间的动作?

时间:2018-01-11 07:55:52

标签: ios sprite-kit

我试图让我的播放器更改图像,一旦它检测到我的某个游戏物品发生碰撞并在0.3秒后恢复,但我不知道如何为我的图像更改设置0.3持续时间?目前图像闪烁变化

这是我的代码:

func didBegin(_ contact: SKPhysicsContact) {
    var firstBody = SKPhysicsBody()
    var secondBody = SKPhysicsBody()


     if firstBody.node?.name == "Player" && secondBody.node?.name == "Poop" {
        let sound = SKAction.playSoundFileNamed("coinsound", waitForCompletion: false)
        run(sound)
        score += 1

        // Changes's player image to Poop once it collides with "Poop"
        let poopImage = SKTexture(imageNamed: "Poop")
        let action = SKAction.setTexture(poopImage)
        firstBody.node?.run(action)


        scoreLabel?.text = String(score)
        secondBody.node?.removeFromParent()
   }
}




  // Changes player's image back to what it was once it starts moving
  private func managePlayer(){
    if canMove{
        player?.move(left: moveLeft)
        player?.texture = SKTexture(imageNamed:"Player")
    }
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在didBegin(_ contact)中,你的动作按照这样的顺序

let sequence = SKAction.sequence([firstTextureChangeAction, SKAction.wait(forDuration:0.3), secondTextureChangeAction])
firstBody.node?.run(sequence)