我试图使第二个计数器在GameScene.update()中运行,并在我按下按钮达5秒钟时进行计数,然后停止并重置为0,以等待再次按下按钮。

这是我按下按钮的代码:

override func update(currentTime: CFTimeInterval) {

    if (Data.gameScene.buttonPressed == true) {
        player.texture = SKTexture(imageNamed: "Spaceship2")
        player.physicsBody = nil
    }
}

如您所见,按下按钮时,播放器变为不可见(Spaceship2不透明),并且不再具有物理物体(因此敌人无法击打它)。

我想要的是这个(用伪代码):
if (button pressed == true) {
    start timer
    if timer < 5 seconds {
        player can't be hit and is invisible
    } else {
         player can be hit and is visible
      }
 }

有人可以帮我吗?

最佳答案

您可以通过一系列的wait操作和runBlock操作来完成此操作。

override func update(currentTime: CFTimeInterval) {

    if (Data.gameScene.buttonPressed == true) {
        //put code to make the player invisible

       let waitAction = SKAction.waitForDuration(5)
       let makePlayerVisibleAction = SKAction.runBlock {
           //put code to make the player visible
       }
       let action = SKAction.sequence([waitAction, makePlayerVisibleAction])

       player.runAction(action)
    }
}

关于ios - 我如何使int秒计数器在update()中按下按钮时启动?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29203721/

10-10 20:34