我已经创建了一个类似于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()
}
}
}发布于 2016-12-20 00:38:55
在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更改以满足您的需求。您必须将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:()
}
}
}发布于 2016-12-20 09:35:39
我通常制作自己的计时器,并将其放在.update()中,用于不需要精确控制的事情……这里有一些动作和增量时间函数。我重写了这个答案,使其比以前的更简单:
下面,我们为几个变量设置了一个名称空间,枚举计时器,并使用它来更新我们的时钟,还使用它作为一个逻辑状态,决定我们是否应该对restartButton单击采取行动。
我们的帧率假设为60,因此更新称为60计时器每秒。因此,我们每次都会添加一个“滴答”,在60次滴答之后,我们知道已经有了第二个滴答。这并不准确,在低于60帧速率的游戏中,你需要做一些增量时间的数学运算来获得正确的时间(或者使用另一种方法)
但在这个简单的例子中,我将它简化为一个基本的计时器,这样你就可以显式地控制场景中的一切。
基本上,一旦我们达到3秒,它将是time.toPlay = true,从而使条件语句"true“,从而允许我们执行您创建的click()函数。之后,它会将time.toPlay重置为false,这样,如果您再次单击该按钮,则不会发生任何事情:
这实际上给了你3秒钟的时间,不允许你触摸按钮,因为它淡入和不被允许。就我个人而言,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 }发布于 2016-12-19 23:53:46
您可以使用函数等待几秒钟,然后调用函数createButton()
您可以像这样使用
self.perform(#selector(self.createButton), with: nil, afterDelay: 2.0)然后,它将被称为2秒之后。
https://stackoverflow.com/questions/41225287
复制相似问题