gpt4 book ai didi

swift - 试图让我的重启按钮保持空闲 2 - 3 秒

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:01 25 4
gpt4 key购买 nike

我制作了一款类似于 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:()
}

}
}

关于swift - 试图让我的重启按钮保持空闲 2 - 3 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41225287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com