如何在精灵套件中的按钮上设置冷却时间

how to set a cool down on a button in sprite kit

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        let positionInScene = touch!.location(in: self)
        let touchedNode = self.atPoint(positionInScene)

            if let name = touchedNode.name {
                if name == "jumpbutton" {
                    let wait = SKAction.wait(forDuration: 3)
                    let boost = SKAction.applyImpulse(CGVector(dx: 0.0, dy: 160.0), duration: 0.1)
                            let action = SKAction.group([boost, wait])
                    player!.run(action)
                    
                    print("jumpbutton")
                }

这是我的代码,我试图在再次按下跳跃按钮之前设置 3 秒的冷却时间。感谢您的帮助。

您可以像您倾向于做的那样简单地执行操作:

// Disable button
let waitAction = SKAction.wait(forDuration: coolDownTime)
run(waitAction) { [weak self] in
  // Enable button
}

为了扩展这个答案,我想建议创建一个组件,这样您就不需要处理 GameScene 上的每个按钮按下。

首先在文件上创建按钮 SKButton.swift:

导入 SpriteKit

class SKButton: SKSpriteNode {
    
    var coolDownTime: TimeInterval = 2.0
    var isReady: Bool = true
    private var originalColor: UIColor = .red
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if isReady {
            print("This button was touched")
            originalColor = color
            color = .gray
            isReady = false
            let waitAction = SKAction.wait(forDuration: coolDownTime)
            run(waitAction) { [weak self] in
                self?.isReady = true
                self?.color = self?.originalColor ?? .blue
            }
        }
    }
    
}

这是它在我的 GameScene 中工作的一个例子:

import SpriteKit

class GameScene: SKScene {
    
  
    override func didMove(to view: SKView) {
        super.didMove(to: view)
        
        let myButton = SKButton()
        myButton.position = CGPoint(x: frame.midX, y: frame.midY)
        myButton.color = .red
        myButton.size = CGSize(width: 200, height: 200)
        myButton.isUserInteractionEnabled = true
        
        addChild(myButton)
    }
    
}

有效的是创建一个标志来确定按下跳跃按钮是允许我们跳跃还是不允许我们跳跃(即它正在冷却)。 为此,您在 class 顶部附近创建一个名为 isReady 的变量,并将其设置为 true。

当涉及到触摸时,我们要检查我们正在触摸的节点是否是跳转按钮(touchedNode.name == "jump button"),以及按钮是否准备好(isReady == true)。如果两者都为真,我们可以继续。我们首先设置标志isReady = false,这样我们就不能立即再次运行这段代码(想象有人快速按下跳转按钮)。

下一部分是创建提升并运行调整它。

最后,我们创建一个持续时间为 3 秒的等待动作。我们 运行 这个等待动作,然后在完成时我们将标志设置回真 (isReady = true)。

class GameScene: SKScene {

  var isReady = true

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let touch = touches.first
    let positionInScene = touch!.location(in: self)
    let touchedNode = self.atPoint(positionInScene)

    if touchedNode.name == "jumpbutton" && isReady == true {
     
            isReady = false
      
            let boost = SKAction.applyImpulse(CGVector(dx: 0.0, dy: 160.0), duration: 0.1)
            player!.run(boost)
        
            print("jumpbutton")
      
            let wait = SKAction.wait(forDuration: 3)
            run(wait) { [self] in
              isReady = true
            }
    }
  }
}