Swift Singleton用于预加载委托中的纹理而不将数据传输到其他类

时间:2015-07-10 02:21:37

标签: swift sprite-kit singleton textures preload

我正在制作一个SpriteKit游戏,我正在尝试使用单例来预加载AppDelegate中的所有纹理,以便稍后我可以在游戏场景中使用带有纹理的类的实例。纹理都在委托中的单例中正确保存,但是当我尝试在我的游戏场景中稍后获取该实例时,我的单例中的SKTexture数组是空的。以下是我AppDelegate

中的代码
func preLoadAllAssets() {
    var assets = Assets()   //make instance of the assets

    let monkiesAtlas = SKTextureAtlas(named: "Monkies")
    let monkiesAtlasLeft = SKTextureAtlas(named: "MonkiesLeft")
    let bananaAnimatedAtlas = SKTextureAtlas(named: "Banana")
    let asteroidAtlas1 = SKTextureAtlas(named: "SmallAsteroids")
    let asteroidAtlas2 = SKTextureAtlas(named: "MediumAsteroids")
    let asteroidAtlas3 = SKTextureAtlas(named: "LargeAsteroids")
    let menuAtlas = SKTextureAtlas(named: "Menu")

    SKTextureAtlas.preloadTextureAtlases([monkiesAtlas, monkiesAtlasLeft, bananaAnimatedAtlas, asteroidAtlas1, asteroidAtlas2, asteroidAtlas3, menuAtlas], withCompletionHandler: {
            //initialize the different textures
            assets.initAtlases(monkiesAtlas, monkiesAtlasLeft: monkiesAtlasLeft, bananaAnimatedAtlas: bananaAnimatedAtlas, asteroidAtlas1: asteroidAtlas1, asteroidAtlas2: asteroidAtlas2, asteroidAtlas3: asteroidAtlas3, menuAtlas: menuAtlas)
            assets.initMonkies()
            assets.initLeftMonkies()
            assets.initBananaFrames()
            assets.initSmallAsteroids()
            assets.initMediumAsteroids()
            assets.initLargeAsteroids()
            assets.initMenu()
            println("Finished preloading textures")
        })
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    Fabric.with([Crashlytics()])

    //set up the assets
    preLoadAllAssets()

    return true
}

这是我的单身人士Assets

class Assets : SKScene {

static let sharedInstance = Assets()

var monkiesAtlas = SKTextureAtlas()
var monkiesAtlasLeft = SKTextureAtlas()
var bananaAnimatedAtlas = SKTextureAtlas()
var asteroidAtlas1 = SKTextureAtlas()
var asteroidAtlas2 = SKTextureAtlas()
var asteroidAtlas3 = SKTextureAtlas()
var menuAtlas = SKTextureAtlas()

//monkey textures that will be used in the scene
var monkeyTextures:[SKTexture]!
var monkeyLeftTextures:[SKTexture]!
var bananaFrames:[SKTexture]!
var smallAsteroidTextures:[SKTexture]!
var mediumAsteroidTextures:[SKTexture]!
var largeAsteroidTectures:[SKTexture]!
var menuTextures:[SKTexture]!

func initAtlases(monkiesAtlas:SKTextureAtlas, monkiesAtlasLeft:SKTextureAtlas, bananaAnimatedAtlas:SKTextureAtlas, asteroidAtlas1:SKTextureAtlas, asteroidAtlas2:SKTextureAtlas, asteroidAtlas3:SKTextureAtlas, menuAtlas:SKTextureAtlas) {
    self.monkiesAtlas = monkiesAtlas
    self.monkiesAtlasLeft = monkiesAtlasLeft
    self.bananaAnimatedAtlas = bananaAnimatedAtlas
    self.asteroidAtlas1 = asteroidAtlas1
    self.asteroidAtlas2 = asteroidAtlas2
    self.asteroidAtlas3 = asteroidAtlas3
    self.menuAtlas = menuAtlas
}

//init the monkies textures
func initMonkies() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let monkiesAtlas = SKTextureAtlas(named: "Monkies")
    var tempMonkeyTextures = [SKTexture]()
    let numImages = monkiesAtlas.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempMonkeyTextures.append(monkiesAtlas.textureNamed("monkey\(i)"))
    }
    //return tempMonkeyTextures
    self.monkeyTextures = tempMonkeyTextures
}

//init the monkies textures
func initLeftMonkies() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let monkiesAtlas = SKTextureAtlas(named: "MonkiesLeft")
    var tempMonkeyTextures = [SKTexture]()
    let numImages = monkiesAtlas.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempMonkeyTextures.append(monkiesAtlas.textureNamed("MonkeyLeft\(i)"))
    }
    //return tempMonkeyTextures
    self.monkeyLeftTextures = tempMonkeyTextures
}

//set up the banana frames
func initBananaFrames() /*-> [SKTexture]*/ {
    //let bananaAnimatedAtlas = SKTextureAtlas(named: "Banana")
    var tempBananaFrames = [SKTexture]()
    let numImages = bananaAnimatedAtlas.textureNames.count
    for var i = 1; i <= numImages; i++ {
        tempBananaFrames.append(bananaAnimatedAtlas.textureNamed("banana\(i)"))
    }
    //return tempBananaFrames
    self.bananaFrames = tempBananaFrames
}

//init the asteroids textures
func initSmallAsteroids() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let asteroidAtlas1 = SKTextureAtlas(named: "SmallAsteroids")
    var tempAsteroidTextures = [SKTexture]()
    let numImages = asteroidAtlas1.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempAsteroidTextures.append(asteroidAtlas1.textureNamed("asteroid\(i)"))
    }
    //return tempAsteroidTextures
    self.smallAsteroidTextures = tempAsteroidTextures
}

//init the asteroids textures
func initMediumAsteroids() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let asteroidAtlas2 = SKTextureAtlas(named: "MediumAsteroids")
    var tempAsteroidTextures = [SKTexture]()
    let numImages = asteroidAtlas2.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempAsteroidTextures.append(asteroidAtlas2.textureNamed("asteroid\(i)"))
    }
    //return tempAsteroidTextures
    self.mediumAsteroidTextures = tempAsteroidTextures
}

//init the asteroids textures
func initLargeAsteroids() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let asteroidAtlas3 = SKTextureAtlas(named: "LargeAsteroids")
    var tempAsteroidTextures = [SKTexture]()
    let numImages = asteroidAtlas3.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempAsteroidTextures.append(asteroidAtlas3.textureNamed("asteroid\(i)"))
    }
    //return tempAsteroidTextures
    self.largeAsteroidTectures = tempAsteroidTextures
}

//init the menu textures
func initMenu() /*-> [SKTexture]*/ {
    //get textures from the atlas
    //let menuAtlas = SKTextureAtlas(named: "Menu")
    var tempMenuTextures = [SKTexture]()
    let numImages = menuAtlas.textureNames.count
    for var i = 1; i <= numImages; i++ {                //put all the atlas images into array of textures
        tempMenuTextures.append(menuAtlas.textureNamed("menu\(i)"))
    }
    //return tempMenuTextures
    self.menuTextures = tempMenuTextures
}

}

这就是我在sharedInstance

中使用单身GameScene的地方
self.monkeyTextures = Assets.sharedInstance.monkeyTextures
self.monkeyLeftTextures = Assets.sharedInstance.monkeyLeftTextures
self.bananaFrames = Assets.sharedInstance.bananaFrames
self.smallAsteroidTextures = Assets.sharedInstance.smallAsteroidTextures
self.mediumAsteroidTextures = Assets.sharedInstance.mediumAsteroidTextures
self.largeAsteroidTextures = Assets.sharedInstance.largeAsteroidTectures
self.menuTextures = Assets.sharedInstance.menuTextures

正如您所看到的,我正在从单身人士中设置游戏场景中的[SKTexture]个变量。这是他们是空的,游戏崩溃。我的单身人员做错了什么?这是很多代码所以请提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

在didFinishLaunchingWithOptions中将所有内容加载到AppDelegate中是一个坏主意。如果您的加载时间过长,或者应用程序将被系统杀死,您可能会被拒绝。您可以将加载场景作为第一个视图,然后从单个类中加载它。也可以尝试单身人士。

    private let _sharedInstance = Assets()

    class Assets {        
         class var sharedInstance : Assets {
           return _sharedInstance
         } 

    // The rest of your code here
    }