从GameViewController中的NSTimer调用GameScene中定义的函数

时间:2016-05-26 04:44:24

标签: ios swift

在我的GameViewController.swift中,我有一个名为resumeGameScene的函数。在那个函数中,我启动了3个计时器。所有这些都在我的GameScene.swift文件中调用了3个独立的函数(lane1,lane2和lane3)。我已经尝试了制作我的函数类函数但是这没有用,因为我在这些函数中有更多的函数和变量(lane1,lane2和lane3)。

GameViewController.swift:

    func resumeGameScene() {
    if (GameScene.unarchiveFromFile("GameScene") as? GameScene) != nil
    {
        let skView = self.view as! SKView

        if skView.scene != nil
        {
            skView.paused = false

            if resumeTime1 != nil && resumeTime2 != nil && resumeTime3 != nil
            {

                timer1 = NSTimer.scheduledTimerWithTimeInterval(resumeTime1!, target: self, selector: #selector(GameScene.lane1), userInfo: nil, repeats: false)

                timer2 = NSTimer.scheduledTimerWithTimeInterval(resumeTime2!, target: self, selector: #selector(GameScene.lane2), userInfo: nil, repeats: false)

                timer3 = NSTimer.scheduledTimerWithTimeInterval(resumeTime3!, target: self, selector: #selector(GameScene.lane3), userInfo: nil, repeats: false)



            }

        }
    }
}

GameScene.swift:

func lane1()
{


    let randomNum = arc4random_uniform(100) + 1

    levels()

    if randomNum < 46
    {
        mouseFire(1, laneFire: fire)

    }
    else
    {
        enemyFire(1, laneFire: fire)

    }

}//End of lane1 func


func lane2()
{

    let randomNum = arc4random_uniform(100) + 1

    levels()

    if randomNum < 46
    {
        mouseFire(3, laneFire: fire)

    }
    else
    {
        enemyFire(3, laneFire: fire)

    }

}//End of Lane2 func


func lane3()
{


    let randomNum = arc4random_uniform(100) + 1

    levels()

    if randomNum < 46
    {
        mouseFire(5, laneFire: fire)

    }
    else
    {
        enemyFire(5, laneFire: fire)

    }

}//End of Lane3 func

1 个答案:

答案 0 :(得分:0)

您的目标是self,因此self上的GameViewController会调用选择器。由于您的选择器不在GameViewController中,因此无效。您需要创建GameScene的对象并将其传递给目标。