如何在Swift中创建一个Global函数

时间:2023-01-02 16:45:19

I am trying to get this function to be know by all controllers but i don't know how to put a function as a global.

我试图让所有控制器知道这个功能,但我不知道如何把一个函数作为一个全局。

import AVFoundation
import Foundation
import SpriteKit
import GameKit
import UIKit

class GameScene: SKScene {
    var SoundMute = SKSpriteNode()

    override func didMoveToView(view: SKView) {

    }

    func CheckMusicMute() {
        if InGameMusicOnOff == true {
            SoundMute.texture = SKTexture(imageNamed: "SilverCircle.png")!     
        }
        if InGameMusicOnOff == false {
            SoundMute.texture = SKTexture(imageNamed: "RedCircle.png")!
        }
    }
}

1 个解决方案

#1


0  

You can create a custom class with the method you need like this:

您可以使用您需要的方法创建自定义类,如下所示:

class MyScene: SKScene {
    func CheckMusicMute() {
        if InGameMusicOnOff == true {
            SoundMute.texture = SKTexture(imageNamed: "SilverCircle.png")!
        }
        if InGameMusicOnOff == false {
            SoundMute.texture = SKTexture(imageNamed: "RedCircle.png")!
        }
    }
}

Then make GameScene extend MyScene:

然后让GameScene扩展MyScene:

class GameScene: MyScene

#1


0  

You can create a custom class with the method you need like this:

您可以使用您需要的方法创建自定义类,如下所示:

class MyScene: SKScene {
    func CheckMusicMute() {
        if InGameMusicOnOff == true {
            SoundMute.texture = SKTexture(imageNamed: "SilverCircle.png")!
        }
        if InGameMusicOnOff == false {
            SoundMute.texture = SKTexture(imageNamed: "RedCircle.png")!
        }
    }
}

Then make GameScene extend MyScene:

然后让GameScene扩展MyScene:

class GameScene: MyScene