簡體   English   中英

Swift SpriteKit為GameScene添加計時器

[英]Swift SpriteKit Add timer to GameScene

我瀏覽了許多文章,但似乎沒有任何意義或與我的問題相關。 我想給用戶設定時間來按下一個節點。 如果他們在設定時間內成功按下節點,計時器應重置;如果他們未能在設定時間內按下節點,游戲將結束。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        if square.contains(touch.location(in: self)) {
            moveSquare()
            GameScene.score+=1
            scoreLabel.text = "\(GameScene.score)"
            }
        } else {
            gameOverScene()
        }
}

所以我的問題是如何設置計時器以滿足這些要求,並將代碼放在哪里?

使用SKActions代替計時器:

let countdown = SKAction.sequence([SKAction.wait(forDuration: 5),
                                   SKAction.perform(#selector(gameOver), onTarget: Self)])
run(countdown, withKey: "gameOverTimer")

(這假設如果未及時觸摸正確的按鈕,則gameOver函數是您要調用的函數)

並在touchesBegan ,如果觸摸了正確的節點:

removeAction(forKey: "gameOverTimer")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM