加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 尝试让我的重启按钮保持空闲状态2到3秒

发布时间:2020-12-14 04:54:04 所属栏目:百科 来源:网络整理
导读:我创造了一个类似Flappy Bird的游戏.我已经设置了一个函数,以便在英雄死亡时调用restartScene.触摸时,这将重新启动游戏,以便用户可以继续玩游戏. 我的问题是,是否有可能在用户点击重启之前延迟2-3秒? func restartScene(){ self.removeAllChildren() self.r
我创造了一个类似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:()
    }

  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读