我制作了一款类似于 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),enable]), 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 更改以满足您的需求。您必须根据案例陈述中的内容命名您的游戏场景才能使其正常工作。

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:() 
    } 
 
  } 
} 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

ios之将选择器转发到 Swift 中的多个对象