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

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

概述我创造了一个类似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 happenrestartbutton.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秒所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1014498.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存