swift - 游戏倒数计时器几乎正常工作

时间:2015-08-15 18:07:10

标签: ios swift nstimer

我使用脚本(下方),作为我游戏开始的倒计时,我使用的脚本来自Gourav Nayyar的YouTube视频,并且第一次使用效果很好叫做。但是,一旦游戏完成重置过程并再次调用脚本,我只会看到5而不是5 - 4 - 3 - 2 - 1 - GO!。如果我从我的脚本中删除其中一个cals,那么它可以在reset func或gameScene加载时正常工作。

以下是GameScene.swift

中的两个电话
override func didMoveToView(view: SKView) {

    var gamelaunchTimerView:TimerView = TimerView.loadingCountDownTimerViewInView(self.view!)
    gamelaunchTimerView.startTimer()

func resetScene() {

    //code removed from here
    return countdown()

}

func countdown() {
    var gamelaunchTimerView:TimerView = TimerView.loadingCountDownTimerViewInView(self.view!)
    gamelaunchTimerView.startTimer()

}

以下是GameLaunchTimer.swift中的定时器代码,因为设置倒计时仅在首次呼叫时起作用并在第二次呼叫时挂起。

//
//  TimerView.swift
//  GameLaunchTimer
//
//  Created by Gourav Nayyar on 7/3/14.
//  Copyright (c) 2014 Gourav Nayyar. All rights reserved.
//

let VIEW_ALPHA:CGFloat = 0.5
let TIMERVIEW_RADIUS:CGFloat = 50
let TIMER_LABEL_INITIAL_VAL:Int =  5
let BORDER_WIDTH:CGFloat = 2
var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
var timer:NSTimer!


import UIKit
import QuartzCore

class TimerView :UIView {

struct Stored {
static var timerLbl:UILabel!
}


class func loadingCountDownTimerViewInView (_superView:UIView)-> TimerView
{
var timerView:TimerView = TimerView(frame:_superView.frame)
//   timerView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(VIEW_ALPHA)
_superView.addSubview(timerView)

/* add a custom Circle view */

let refFrame:CGRect = CGRectMake(_superView.center.x-TIMERVIEW_RADIUS, _superView.center.y-TIMERVIEW_RADIUS, 2*TIMERVIEW_RADIUS, 2*TIMERVIEW_RADIUS)
var circleView:UIView = UIView(frame:refFrame)
circleView.layer.cornerRadius = TIMERVIEW_RADIUS
circleView.layer.borderColor = UIColor.whiteColor().CGColor
circleView.layer.borderWidth = BORDER_WIDTH

/* add a custom Label */

Stored.timerLbl = UILabel(frame:circleView.bounds)
Stored.timerLbl.text = "\(TIMER_LABEL_INITIAL_VAL)"
Stored.timerLbl.textColor = UIColor.whiteColor()
Stored.timerLbl.font = UIFont(name: "MarkerFelt-Thin", size: 40)
Stored.timerLbl.textAlignment = NSTextAlignment.Center

circleView.addSubview(Stored.timerLbl)
timerView.addSubview(circleView)

return timerView
}

func startTimer()
{
timer = NSTimer.scheduledTimerWithTimeInterval(1.0
  , target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
}

func updateTimer(dt:NSTimer)
{
timerVal--
if timerVal==0{
  Stored.timerLbl.text = "GO!"
}else if timerVal<0{
  timer.invalidate()
  removeCountDownTimerView()
} else{
  Stored.timerLbl.text = "\(timerVal)"
}
}

func removeCountDownTimerView()
{
var mySuperView:UIView = self.superview!
mySuperView.userInteractionEnabled = true
super.removeFromSuperview()
}
}

2 个答案:

答案 0 :(得分:1)

在类体下定义变量;

import UIKit
import QuartzCore

class TimerView :UIView {

let VIEW_ALPHA:CGFloat = 0.5
let TIMERVIEW_RADIUS:CGFloat = 50
let TIMER_LABEL_INITIAL_VAL:Int =  5
let BORDER_WIDTH:CGFloat = 2
var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
var timer:NSTimer!

... // other code

或者,可能是

    let VIEW_ALPHA:CGFloat = 0.5
    let TIMERVIEW_RADIUS:CGFloat = 50
    let TIMER_LABEL_INITIAL_VAL:Int =  5
    let BORDER_WIDTH:CGFloat = 2

    import UIKit
    import QuartzCore

    class TimerView :UIView {

    var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
    var timer:NSTimer!
    ... //other code

答案 1 :(得分:0)

在使计时器失效后将nil分配给计时器:即使您无效,仍会保留对象状态,导致在创建计时器的新实例时状态发生冲突,一旦它在不同的线程中运行。