2017-06-24 11 views
1

他の回答を検索しようとしましたが、私のシナリオに該当するものは見つかりませんでした。私は迅速にゲームを書いており、プレイヤーがどれくらいプレイしているかを決めるストップウォッチを作りたいと思っています。ユーザーがタッチすると、ストップウォッチが始まり、特定の動作が起こると、タイマーは停止してリセットされます。分、秒、ミリ秒(つまり、00:00.00)を使用したいと思います。すぐにストップウォッチを作成する

現在、機能の時間機能の種類。 0から始まらず、現在の秒で開始します(私は起動時にそれを知っていますが、0で起動する方法はわかりません)。また、画面に触れると更新されるだけです。私はそれを00:00.00からカウントし、キャンセルアクションが発生するまでそれ自身で更新する必要があります。

ありがとうございます。ここで

は、私がこれまで持っているものです。

class GameScene: SKScene { 

var activeTimer = SKLabelNode() 
//var bestTime = SKLabelNode() 

var startTime = TimeInterval() 
//var currentTime = TimeInterval() 
//var countingTime = TimeInterval() 
var updateTimerAction = SKAction() 


override func didMove(to view: SKView) { 

    activeTimer = self.childNode(withName: "active_timer") as! SKLabelNode 
    //bestTime = self.childNode(withName: "best_time") as! SKLabelNode 

    startTime = TimeInterval(Calendar.current.component(.second, from:Date())) 
    updateTimerAction = SKAction.sequence([SKAction.run(updateTimer), SKAction.wait(forDuration: 1.0)]) 
} 


func startGame() { 
    // Other code 
    startGameTimer() 
} 

func resetGame() { 
    // Other code 
    stopGameTimer() 
} 

func startGameTimer() { 
    run(SKAction.repeatForever(updateTimerAction), withKey: "timer") 
} 

func updateTimer() { 
    activeTimer.text = stringFromTimeInterval(interval: startTime) as String 
} 

func stringFromTimeInterval(interval: TimeInterval) -> NSString { 

    let ti = NSInteger(interval) 

    let ms = Int((interval.truncatingRemainder(dividingBy: 1)) * 1000) 

    let seconds = ti % 60 
    let minutes = (ti/60) % 60 

    return NSString(format: "%0.2d:%0.2d.%0.2d",minutes,seconds,ms) 
} 


func stopGameTimer() { 
    removeAction(forKey: "timer") 
} 

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

    startGame() 

    for touch in touches { 
     // other code 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 
     // other code 
    } 
} 


override func update(_ currentTime: TimeInterval) { 

    updateTimer() 

    if <action> { 
     // stop timer and reset game 
     resetGame() 
    } 
} 

答えて

2

代わりに時間間隔オブジェクトを使用するのでは、ラベルの値をインクリメント機能(または内部を呼び出し、Timerオブジェクトを使用する方がよいかもしれません変数)を1秒ごとに0以上にします(または小さくすることもできます)。クロックを停止するには、Timerオブジェクトのinvalidate関数を呼び出します。 詳細については、ドキュメントを確認してください:https://developer.apple.com/documentation/foundation/timer

+0

私はhttps://stackoverflow.com/questions/23978209/spritekit-creating([HERE]私はこの質問に応じたゲームシーンにタイマー(以前NSTimer)を使用すると仮定していなかった印象の下にありました-a-timer/23978854#23978854) –

+0

これは正しいですが、Timer *は簡単なゲームで動作しますが、最終的にはSpriteKitでいくつかの問題が発生します。このためにSKAction.waitなどを使用するのが最善です – Fluidity

+0

@Fluidity私は彼が言っているとは思わない。 Timerオブジェクトを使うべきだと教えてくれました。 Timerオブジェクトを使用しないでください。私は、SKSceneでTimerオブジェクトを使用しないと言います。 –

2

次のコード行は、私がMac App Storeに3週間前に提出した実際のmacOSゲームに由来しています。それはあなたのケースでは0に数をカウントダウン、

self.currentSeconds += 1 

timeBackNode

self.currentSeconds -= 1 

を変更SKLabelNodeがtimeNode0とtimeNode1オブジェクト保持している空のノードです。したがって、ゲームがdidMoveで始まるときに作成してください。

var currentSeconds = Int() // currentSeconds 
var timeNode0 = SKLabelNode() // timeNode0 
var timeNode1 = SKLabelNode() // timeNode1 
func setupTimeGoal() { 
    enumerateChildNodes(withName: "//TimerBack", using: { timeBackNode, _ in 
     let goal = self.timeDict["num"] as! Int 
     self.currentSeconds = goal // In your case, goal is 0 since you are going to count up. 
     self.timeNode0 = SKLabelNode(fontNamed: "Your font's font name") 
     self.timeNode0.text = self.makeTimeWithSeconds(secs: goal) 
     self.timeNode0.fontSize = 26 
     self.timeNode0.fontColor = SKColor.black 
     self.timeNode0.horizontalAlignmentMode = .center 
     self.timeNode0.position = CGPoint(x: 1, y: -22) 
     timeBackNode.addChild(self.timeNode0) 

     self.timeNode1 = SKLabelNode(fontNamed: "Your font's font name") 
     self.timeNode1.text = self.makeTimeWithSeconds(secs: goal) // this function translate a counting number into a time code like 00:00:00 
     self.timeNode1.fontSize = 26 
     self.timeNode1.fontColor = SKColor.cyan 
     self.timeNode1.horizontalAlignmentMode = .center 
     self.timeNode1.position = CGPoint(x: 0, y: -23) 
     timeBackNode.addChild(self.timeNode1) 
    }) 
} 

func repeatTime() { 
    let waitAction = SKAction.wait(forDuration: 1.0) 
    let completionAction = SKAction.run { 
     if self.currentSeconds == 0 && self.gameState == .playing { 
      self.removeAction(forKey: "RepeatTime") 
      self.proceedLoss() 
      return 
     } 
     self.currentSeconds -= 1 
     self.timeNode0.text = self.makeTimeWithSeconds(secs: self.currentSeconds) 
     self.timeNode1.text = self.makeTimeWithSeconds(secs: self.currentSeconds) 
    } 
    let seqAction = SKAction.sequence([waitAction, completionAction]) 
    let repeatAction = SKAction.repeatForever(seqAction) 
    self.run(repeatAction, withKey: "RepeatTime") 
} 
0

これは私がやったことです。 El Tomatoの答えはうまくいくように見えますが、私のコードの設定を考えれば、私は少し別のルートに行きました。他の友人と話した後、私は自分のアプリケーションの設計のためにタイマーを選んだ。 Timerオブジェクトを使用して問題が発生した場合は、この質問を更新します。

以下のコードはタッチで開始し、「00:00.00」の形式で更新し、アクションが発生すると停止します。この時間は別のタッチまで一時停止し、タイマーはゼロから開始します。

class GameScene: SKScene { 

var seconds = 0 
var timer = Timer() 
var timeStarted = Bool() 
var activeTimer = SKLabelNode() 
//var bestTime = SKLabelNode() 

override func didMove(to view: SKView) { 

    activeTimer = self.childNode(withName: "active_timer") as! SKLabelNode 
    //bestTime = self.childNode(withName: "best_time") as! SKLabelNode 
    timeStarted = false 

} 

func startGame() { 

    startGameTimer() 
    timeStarted = true 
} 

func resetGame() { 

    timeStarted = false 
    stopGameTimer() 
    seconds = 0 
} 

func startGameTimer() { 
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true) 
} 

func updateTimer() { 
    seconds += 1 
    activeTimer.text = timeString(time: TimeInterval(seconds)) 
} 

func timeString(time:TimeInterval) -> String { 
    let hours = Int(time)/3600 
    let minutes = Int(time)/60 % 60 
    let seconds = Int(time) % 60 
    return String(format:"%02i:%02i.%02i", hours, minutes, seconds) 
} 


func stopGameTimer() { 
    timer.invalidate() 
    removeAction(forKey: "timer") 
} 

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

    if !timeStarted { 
     startGame() 
    } 

    for touch in touches { 
     // other code 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    for touch in touches { 
     // other code 
    } 
} 


override func update(_ currentTime: TimeInterval) { 

    if timeStarted { 
     updateTimer() 
    } 

    if <action> { 
    // stop timer and reset game 
    resetGame() 
    } 
} 
} 
関連する問題