Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 尝试让我的重启按钮保持空闲状态2到3秒大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我创造了一个类似Flappy Bird的游戏.我已经设置了一个函数,以便在英雄死亡时调用restartScene.触摸时,这将重新启动游戏,以便用户可以继续玩游戏. 我的问题是,是否有可能在用户点击重启之前延迟2-3秒? func restartScene(){ self.removeAllChildren() self.removeAllActions() died =
我创造了一个类似Flappy Bird的游戏.我已经设置了一个函数,以便在英雄死亡时调用restartScene.触摸时,这将重新启动游戏,以便用户可以继续玩游戏.

我的问题是,是否有可能在用户点击重启之前延迟2-3秒?

func restartScene(){

    self.removeAllChildren()
    self.removeAllActions()
    died = false
    gameStarted = false
    score = 0
    createScene()

}

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

        gameStarted = true

  for touch in touches{
        let LOCATIOn = touch.LOCATIOn(in: self)

        if Died == true{


            restartScene()
        }

    }
}

解决方法

在createButton末尾使用SKAction执行延迟:

场景1,您正在使用SKScene来处理所有触摸事件(这是您必须要做的事情,因为restartButton是一个SKSpriteNodE)

let enable = SKAction.run({[uNowned self] in self.restartButton.isUserInteractionEnabled = falsE})

restartButton.isUserInteractionEnabled = true //This actually disables the button because the touch handler will not be called by scene,but instead the individual button. 
//The individual button will have no touch code associated with it,so nothing will happen

restartButton.run(SKAction.sequence([SKAction.wait(duration:2),enable]),withKey:"waiTingToEnable")

方案2,您使用restartButton作为自定义类:

let enable = SKAction.run({[uNowned self] in self.restartButton.isUserInteractionEnabled = truE})

restartButton.isUserInteractionEnabled = false //This disables the button because the touch handler will not be called by individual button,and instead will go to whatever is touch enabled under it. 
restartButton.run(SKAction.sequence([SKAction.wait(duration:2),withKey:"waiTingToEnable")

在您的特定情况下,我将如何编写它:

func createButton(){

    restartButton = SKSpriteNode(imagenamed: "restart")
    restartButton.position = CGPoint(x: self.frame.width / 2,y: self.frame.height / 2)
    restartButton.zPosition = 10
    restartButton.setScale(1.2)
    restartButton.name = "Restart"
    restartButton.setScale(1.5)
    self.addChild(restartButton)

    let enable = SKAction.run({[uNowned self] in self.restartButton.isUserInteractionEnabled = falsE})

    restartButton.isUserInteractionEnabled = true //This actually disables the button because the touch handler will not be called by scene,but instead the individual button. 
    //The individual button will have no touch code associated with it,so nothing will happen

    let waitAndEnable = SKAction.sequence([SKAction.wait(duration:2),enable])
    let fadeIn = SKAction.fadeIn(withDuration: 1.5)
    let runConcurrently = SKAction.group([waitAndEnable,fadeIn])
    restartButton.run(runConcurrently)

    highscoreLabel.text = "High score: \(UserDefaults().Integer(forKey: "HIGHscore"))"
    highscoreLabel.fontColor = UIColor.white
    highscoreLabel.fontSize = 20
    highscoreLabel.position = CGPoint(x: 80,y: 20)
    highscoreLabel.zPosition = 6

    livesLabel.position = CGPoint(x: frame.size.width / 5.4,y: 30)
    livesLabel.text = "Lives: \(lives)"
    livesLabel.zPosition = 5
    livesLabel.fontSize = 20
    livesLabel.fontColor = UIColor.black
    self.addChild(livesLabel)
    livesLabel.zPosition = 10
}

看起来你没有正确处理touchesBegan.您将其设置为用户触摸场景中的任何位置,游戏将重新启动.

您需要定位特定节点以确保发生这种情况.

添加了touchesBegan更改以满足您的需求.您必须将您的游戏场景命名为case语句中的内容才能使其生效.

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

  for touch in touches{
    let LOCATIOn = touch.LOCATIOn(in: self)
    let node = nodeAtPoint(LOCATIOn)
    switch(node.Name)
    {
      case "Restart":
        if Died = true{
          restartScene()
        }
      case "GameScene": //maybe we want to make this a button?
        gameStarted = true //who cares about a branch statement
      default:()
    }

  }
}

大佬总结

以上是大佬教程为你收集整理的swift – 尝试让我的重启按钮保持空闲状态2到3秒全部内容,希望文章能够帮你解决swift – 尝试让我的重启按钮保持空闲状态2到3秒所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。