我创造了一个类似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()
}
}
}
答案 0 :(得分:1)
使用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:()
}
}
}
答案 1 :(得分:0)
我通常制作自己的计时器并将其放在.update()中,用于那些不需要精确控制的东西......还有动作和Delta-Time功能。我重新设计这个答案比我之前更简单:
下面,我们为一些变量设置命名空间,枚举计时器,并使用它来更新我们的时钟,并将其用作我们是否应该对restartButton
点击进行操作的逻辑状态。
我们的帧率假定为60,因此更新称为每秒60个定时器。因此我们添加一个&#34; tick&#34;每次,在60个滴答之后,我们知道已经有一秒钟。这并不准确,在低于60帧速率的游戏中,您需要进行一些delta时间数学计算以获得正确的时间(或使用其他方法)
但是在这个简单的例子中,我对基本计时器保持简单,这样你就可以明确控制场景中的所有内容。
基本上,一旦我们达到3秒,它将是time.toPlay = true,使得条件语句&#34; true&#34;从而允许我们执行您创建的click()
函数。之后,它会将time.toPlay
重置为false,这样如果您再次单击该按钮,则不会发生任何事情:
这实际上为你提供了3秒不允许触摸按钮,因为它渐渐消失了。个人IMO,2秒将比3更好,特别是因为你的fadein是1.5秒:
// In GameScene field area:
enum time {
static var ticks = 0
static var seconds = 0
static var toPlay = false
}
// In update():
time.ticks += 1
if time.ticks >= 60 { time.seconds += 1; time.ticks = 0 }
if time.seconds >= 2 { time.seconds = 0; time.toPlay = true }
// In the code block where you detect restartButton:
if time.toPlay {
// Call your restartGame function here:
// ...
} else { return }
答案 2 :(得分:-2)
您可以使用函数等待几秒钟,然后调用函数createButton()
你可以这样使用
self.perform(#selector(self.createButton), with: nil, afterDelay: 2.0)
然后,它将在2s后调用。