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

时间:2015-09-19 20:47:24

标签: ios swift

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

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 个答案:

答案 0 :(得分:0)

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

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

然后让GameScene延长MyScene

class GameScene: MyScene