如何实现一个 SpriteKit 计时器?

编程入门 行业动态 更新时间:2024-10-26 22:20:14
本文介绍了如何实现一个 SpriteKit 计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前正在尝试为我的精灵套件游戏实现一个计时器,但我没有让它工作.定时器的初始值始终保持不变.

I am currently trying to implement a timer for my sprite kit game, but I don't get it working. The initial value of the timer always remains the same.

我假设我需要以某种方式/某处更新标签,但我不知道如何和在哪里:?我不明白重点.有什么想法吗?

I am assuming I need to update the label somehow/somewhere, but I don't know HOW and WHERE :? I don't get the point. Any ideas?

这是我在 GameScene 类中的代码

Here is my code within my GameScene Class

    let levelTimerLabel = SKLabelNode(fontNamed: "Chalkduster")
var levelTimerValue: Int  = 500

var levelTimer = NSTimer()

func startLevelTimer() {

    levelTimerLabel.fontColor = SKColor.blackColor()
    levelTimerLabel.fontSize = 40
    levelTimerLabel.position = CGPoint(x: size.width/2, y: size.height/2 + 350)
    addChild(levelTimerLabel)

    levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("levelCountdown"), userInfo: nil, repeats: true)

    levelTimerLabel.text = String(levelTimerValue)

}

func levelCountdown(){

    levelTimerValue--

}

推荐答案

我会坚持使用 SKActions 来处理 SpriteKit 中的这些任务,因为 NSTimer 不是受场景或视图暂停状态的影响,因此可能会导致您陷入困境.或者至少,它会要求您实现暂停功能,以便在某些情况下暂停您的计时器,例如用户暂停场景或接听电话等.阅读更多这里关于SKAction vs NSTimer vs GCDSpriteKit 中与时间相关的操作.

I would stick to SKActions for these kind of tasks in SpriteKit due to fact that NSTimer is not affected by scene's, or view's paused state, so it might lead you into troubles. Or at least, it will require from you to implement a pause feature in order to pause your timers in certain situations, like when user pause the scene, or receive a phone call etc. Read more here about SKAction vs NSTimer vs GCD for time related actions in SpriteKit.

import SpriteKit

class GameScene: SKScene {

var levelTimerLabel = SKLabelNode(fontNamed: "ArialMT")

//Immediately after leveTimerValue variable is set, update label's text
var levelTimerValue: Int = 500 {
    didSet {
        levelTimerLabel.text = "Time left: \(levelTimerValue)"
    }
}

override func didMoveToView(view: SKView) {



levelTimerLabel.fontColor = SKColor.blackColor()
levelTimerLabel.fontSize = 40
levelTimerLabel.position = CGPoint(x: size.width/2, y: size.height/2 + 350)
levelTimerLabel.text = "Time left: \(levelTimerValue)"
addChild(levelTimerLabel)

    let wait = SKAction.waitForDuration(0.5) //change countdown speed here
    let block = SKAction.runBlock({
        [unowned self] in

        if self.levelTimerValue > 0{
            self.levelTimerValue--
        }else{
            self.removeActionForKey("countdown")
        }
    })
    let sequence = SKAction.sequence([wait,block])

    runAction(SKAction.repeatActionForever(sequence), withKey: "countdown")
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    //Stop the countdown action

    if actionForKey("countdown") != nil {removeActionForKey("countdown")}
}
}

这篇关于如何实现一个 SpriteKit 计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 11:29:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393632.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   如何实现   SpriteKit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!