我想从GameViewController调用我的计时器方法(用我的GameScene编写),以便在GameViewController类中初始化的UIButton暂停游戏。
我正在尝试使用这样的class func
:
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
GameScene.startTimer()
}
class func startTimer(){
timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0
, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
}
func updateTimer(dt:NSTimer){
counter--
counterGame++
if counter<0{
timerCount.invalidate()
removeCountDownTimerView()
} else{
labelCounter.text = "\(counter)"
}
if counterGame>20{
balloon.size = CGSizeMake(50, 50)
}
if counterGame>40{
self.physicsWorld.gravity = CGVectorMake(0, -0.8)
}
if counterGame>60{
self.physicsWorld.gravity = CGVectorMake(0, -1)
}
}
func removeCountDownTimerView(){
defaults.setInteger(balloonDestroyed, forKey: "score")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let settingController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("GameOverViewController") as UIViewController
let vc = self.view?.window?.rootViewController
vc?.presentViewController(settingController, animated: true, completion: nil)
}
}
但是这段代码会返回错误:
[Funfair_balloon.GameScene updateTimer:]: unrecognized selector sent to class 0x10b13d940
当我不使用class func
应用程序完美无缺时,我无法使用我的UIButton停止计时器。
我做错了什么?
答案 0 :(得分:0)
请注意,使用class func
时,您无法使用在该类中初始化的变量。
例如,如果变量timerCount
在类GameScene
中初始化,则无法使用变量{{1}}。
答案 1 :(得分:0)
我不确定你为什么要使用类函数。以下应仅使用GameScene
的当前实例。请注意,var timerCount
是可选的(因为您不能轻易覆盖init
),直到您在startTimer()
中创建它为止,因此您必须在展开时将其展开最终使它无效。
class GameScene: SKScene, SKPhysicsContactDelegate {
var timerCount: NSTimer? = nil
var counter = 100 // or whatever
override func didMoveToView(view: SKView) {
self.startTimer()
}
func startTimer() {
self.timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0
, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
}
func updateTimer(dt: NSTimer) {
// Do stuff
counter--
if counter < 0 {
self.timerCount!.invalidate() // or dt.invalidate()
self.removeCountDownTimerView()
} else {
// update counter label
}
// Do more stuff
}
func removeCountDownTimerView() {
// Do stuff
}
}