iOS SpriteKit-游戏开始前的倒计时?

iOS SpriteKit-游戏开始前的倒计时?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,只是在iOS上使用Swift进入SpriteKit。我有一个“突破”的游戏从教程,我想实现倒计时之前,每一个球发射,通过把一个Sk标签在屏幕中部,从5下降到1,然后删除自己和游戏开始。当倒计时开始时,你可以看到整个游戏屏幕上的墙壁、固定球等 我不知道在游戏循环中的什么地方可以这样做。如果我在didMoveToView中倒计时(在那里我创建了墙并初始化了球和桨),我永远不会看到它,但我会在日志中看到我的调试消息。我猜在呈现场景之前会调用didMoveToView 我试图在第一次调用“Update”

只是在iOS上使用Swift进入SpriteKit。我有一个“突破”的游戏从教程,我想实现倒计时之前,每一个球发射,通过把一个Sk标签在屏幕中部,从5下降到1,然后删除自己和游戏开始。当倒计时开始时,你可以看到整个游戏屏幕上的墙壁、固定球等

我不知道在游戏循环中的什么地方可以这样做。如果我在didMoveToView中倒计时(在那里我创建了墙并初始化了球和桨),我永远不会看到它,但我会在日志中看到我的调试消息。我猜在呈现场景之前会调用didMoveToView

我试图在第一次调用“Update”时使用一个标志来调用倒计时函数,但我再次看到在屏幕上出现任何东西之前执行倒计时-我认为在渲染场景之前最初调用了“Update”

我可以在另一个场景中实现一个“点击屏幕开始”屏幕,但我真的希望屏幕上有墙和(静止的)球准备就绪的倒计时。我可以使用游戏屏幕的背景图像来创建这个倒计时场景,但这看起来很尴尬

感谢您的建议


史蒂夫

这正是斯泰默真正派上用场的地方。NSTimer基本上每隔一段时间激活一个函数(您可以指定激活频率)

更新:有时最好不要使用NSTimer;请看这里:

下面是一个使用NSTimer的代码示例:

class ViewController: UIViewController {
var countdownTime = 5
var countdownTimer = NSTimer()
// above, setting your timer and countdown time as global variables so they can be accessed in multiple functions.

func PlayerPressesStart() {
countdownTime = 5
countdownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "Countdown", userInfo: nil, repeats: true) // calls the function "Countdown" every second.
}

func Countdown() { // this is the function called by the timer every second, which causes your "countdownTime" to go down by 1. When it reaches 0, it starts the game.
countdownTime--
if countdownTime > 0 {
countdownTimer.invalidate()
placewhereyoudisplaycountdown.text = String(countdownTime)
}
if countdownTime == 0 {
// call the function where game action begins here, or call the function that makes the game begin here.
}
} // closing bracket for the View Controller, don't include this if you're copying and pasting to your already existing View Controller.

添加了这些函数,并在
didMoveToView

func countdown(count: Int) {
    countdownLabel.horizontalAlignmentMode = .Center
    countdownLabel.verticalAlignmentMode = .Baseline
    countdownLabel.position = CGPoint(x: size.width/2, y: size.height*(1/3))
    countdownLabel.fontColor = SKColor.whiteColor()
    countdownLabel.fontSize = size.height / 30
    countdownLabel.zPosition = 100
    countdownLabel.text = "Launching ball in \(count)..."

addChild(countdownLabel)

let counterDecrement = SKAction.sequence([SKAction.waitForDuration(1.0),
    SKAction.runBlock(countdownAction)])

runAction(SKAction.sequence([SKAction.repeatAction(counterDecrement, count: 5),
    SKAction.runBlock(endCountdown)]))

}

func countdownAction() {
    count--
    countdownLabel.text = "Launching ball in \(count)..."
}

func endCountdown() {
    countdownLabel.removeFromParent()
    ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))
}
因此,我创建并设置了倒计时文本,然后创建并运行SKAction,在倒计时递减和更新标签之前等待1秒。它重复这5次,然后移除倒计时标签,最后给球一个开始移动的冲动,这样游戏就可以开始了


似乎还可以…

当你在谷歌上搜索解决方案时,这从来都不是一个好兆头,最热门的问题是你自己的问题,因此…每次只调用一次更新frame@SteveIves请在下面的NSTimers中查看我的解决方案,如果有任何问题,请告诉我。好的-这里有一点学习经验。我突然想到为什么我的SKSpritelabel倒计时在didMoveToView中有效,而在Update中不起作用-更新循环中的SKLabel不会起作用,因为与UIView中的UILabel不同,在迭代之间标签不会重新渲染。一想就明白了。谢谢约翰。我将尝试一下,看看是否可以在开球前的5秒钟内让SKAction将我的倒计时SKLabel从5减到1(可能是在动作完成后给它一个脉冲)。感谢代码示例John,但是按照建议不要在SpriteKit中使用NSTimer,我直接选择了SKAction技术。作为SpriteKit的新手,我很想知道这是一种好方法还是一种坏方法。。。