我创造了一个类似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:() } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。