【问题标题】:How using class func to call a method from another class in SWIFT如何使用类 func 从 SWIFT 中的另一个类调用方法
【发布时间】:2014-10-13 17:21:45
【问题描述】:

我会从 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 停止计时器。 我做错什么了?

【问题讨论】:

  • 在您的class func startTimer 中,您告诉计时器在timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true) 中调用updateTimer,但类func 中的self 不是实例,因此您不能使用它来调用您的updateTimer,是实例方法,不是类方法。
  • 感谢您的回答。我用 GameScene() 替换了 self。我没有错误了,但计时器不工作。

标签: swift uibutton sprite-kit viewcontroller skscene


【解决方案1】:

请注意,在使用class func 时,您不能使用在该类中初始化的变量。

例如,如果变量timerCountGameScene 类中初始化,则不能使用它。

【讨论】:

  • 所以我必须从GameScene class 中初始化class func 中我需要的所有变量?
  • 要么,要么你将它们作为参数传递到你的函数中。
【解决方案2】:

我不确定你为什么要使用类函数。以下应该只使用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
    }

}

【讨论】:

  • 谢谢。实际上,我有我的 GameScene,其中编写了游戏和计时器。在我的 GameViewController 中,我创建了“暂停”UIButton。当我点击暂停按钮时,我想停止游戏和计时器。目前我可以暂停 skView(不是场景)并使用此按钮使 timerCount 无效,但节点会继续创建,我无法再次启动计时器。这就是为什么我想从 ViewController 访问 GameScene 中的方法。
  • 好吧,我刚刚尝试了您的代码。它可以工作,但是如何使用 pauseButton 从 ViewController 重新启动计时器?
  • 使startTimer 成为一个动作,并将其连接到您的按钮?
  • 好的,我会试试的。 (我还是个菜鸟):-D
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多