在斯威夫特全球函数调用? 如何?(Global function call in Swift?

2019-10-21 04:04发布

我有类方法调用雨燕noobie问题。 我建立使用雪碧套件我的孩子们简单的学习应用程序。 我在GameScene,在那里我几乎做我所有的游戏逻辑定义一个全局变量scoreCount如发现正确答案,并增加scoreCount。 我也有GameOverScene。 在这两个我显示得分-label。 我保持得分与scoreCount参数(在GameScene)的数量。 但是因为我很新的雨燕和雪碧套件,我不知道我应该如何更新GameViewController比分标签? 所以基本上我想从GameScene调用GameViewController.updateLabels()。

我知道这是不是理想的方式,但请您分享您对此的解决方案的概念。 谢谢!

Answer 1:

好。 在你GameViewController,你必须改变你的FUNC类FUNC这样

class func yourFunc {
 //your code
}

若要从GameScene恰好这个代码调用它:

GameViewController.yourFunc()

不要忘记你正在创建一个全球性的功能,所以在它的所有变量必须是全球性的了。

对于您的标签(全球):

var label:UILabel = UILabel()

在您的GameViewController:

class GameViewController : UIViewController {
    override func viewDidLoad() {
    super.viewDidLoad()

    label.text = "bonjour" // to set the text
    label.frame = CGRectMake(0, 0, 100, 100) / set the position AND size CGRectMake (x, y, width, heigth)

    label.textColor = UIColor.redColor() // set your color
    label.center = CGPointMake(self.view.frame.width/2, self.view.frame.height/2) // set the position from x=0 and y=0
    self.view.addSubview(label) // add the label to your screen

我希望这会帮助你。



文章来源: Global function call in Swift? Howto?