【问题标题】:iOS SpriteKit - countdown before game starts?iOS SpriteKit - 游戏开始前的倒计时?
【发布时间】:2016-06-26 21:07:54
【问题描述】:

刚刚使用 Swift 在 iOS 上进入 SpriteKit。我有一个教程中的“Breakout”游戏,我想在每个球发射之前实现倒计时,方法是在屏幕中间放置一个 SKLabel,它从 5 倒数到 1,然后移除自身并开始游戏。在倒计时进行时,您可以看到完整的游戏画面,包括墙壁、固定球等。

我无法确定在游戏循环中的哪个位置执行此操作。如果我在 didMoveToView(我创建墙并初始化球和桨)中进行倒计时,我永远不会看到它,但我会在日志中看到我的调试消息。我猜在 SKScene 出现之前调用了 didMoveToView。

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

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

任何建议都非常感谢,

史蒂夫

【问题讨论】:

  • 当你在谷歌上寻找解决方案时,这绝不是一个好兆头,而最热门的问题是你自己的问题...
  • 每帧只调用一次更新
  • @SteveIves 在下面使用 NSTimers 查看我的解决方案,如果有任何问题,请告诉我。
  • 好的 - 这里有一点学习经验。我突然想到为什么我的 SKSpritelabel 倒计时在 didMoveToView 中起作用但在 Update 中不起作用 - 在 Update 中的循环中更新 SKLabel 将不起作用,因为标签不会在迭代之间重新呈现,这与 UIView 中的 UILabel 不同。想想就明白了。

标签: ios swift sprite-kit


【解决方案1】:

didMoveToView 末尾添加了这些函数和对countdown(5) 的调用

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))
}

所以我创建并设置了倒计时的文本,然后创建并运行一个等待 1 秒的 SKAction,然后减少倒计时并更新标签。它会重复此操作 5 次,然后移除倒计时标签,最后给球一个开始移动的冲动,这样游戏就可以开始了。

似乎工作正常...

【讨论】:

  • 作为 SpriteKit 的新手,我很想知道这是一种好方法还是坏方法......
【解决方案2】:

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

更新: 有时最好不要使用 NSTimer;看这里:https://stackoverflow.com/a/24950982/5700898

这是一个使用 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.

【讨论】:

  • 谢谢约翰。我会试一试,看看我是否可以在开球前 5 秒内让 SKAction 将我的倒计时 SKLabel 从 5 减少到 1(可能是在动作完成时给它一个冲动)。跨度>
  • 感谢 John 提供的代码示例,但按照不要在 SpriteKit 中使用 NSTimer 的建议,我直接使用了 SKAction 技术。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多