簡體   English   中英

如何從GameViewController中的另一個swift文件調用方法?

[英]How can I call a method from another swift file in GameViewController?

我在情節提要中創建了一個按鈕,該按鈕位於游戲屏幕上。 它在GameViewController()中具有IB動作,如下所示:

   @IBAction func buttonPressed(sender: AnyObject) {
        GameScene().myCustomMethod()
    }

在我的GameScene中,存在myCustomMethod(),它將生成敵人,但是上面的代碼無法正常工作。 如果在IBAction中添加println("button was pressed") ,則會在控制台中將其打印出來,但是myCustomMethod將不會執行,並且不會按預期方式產生敵人。

誰能幫助我或解釋如何解決我的問題? 謝謝

在您的方法中,您每次都會創建一個新的GameScene對象。 您僅應創建一次(初始化時),然后始終在此對象上調用myCustomMethod

var gameScene: GameScene!

override func viewDidLoad() {
    gameScene = GameScene()
}

@IBAction func buttonPressed(sender: AnyObject) {
    gameScene.myCustomMethod()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM