简体   繁体   中英

SpriteKit - Preloader using GCD

I am looking for the instructions of how to make an preloader using GCD, but no luck for now. I would like to learn how to preload all the textures and emitters in the background, before scene is initialized. And if possible, update the progress bar animation based on amount of data loaded.

I am looking at Apple's adventure game, and there is an example for similar problem. Actually, the first part is solved. But I can't find anywhere about updating preloader based on the amount of data loaded, so any instructions would be helpful. There must be some obvious way,to calculate, or get info about data loaded in the background queue.

I don't do any progress bars since my levels take about a second to load. I have a seamless transition between levels. Before I was doing things this way my game would freeze for a moment in a transition because it had to load all the resources on the same thread that my game was running

I create a static method in my class to preload resources.

// config is a struct with many parameters
class func createResources(config: HyperSceneConfig, withCompletion: (scene: HyperScene) -> ()){

    // load resources on other thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {

        let hyperScene = HyperScene(config: config)

        // callback on main thread
        dispatch_async(dispatch_get_main_queue(), {
            // Call the completion handler back on the main queue.
            withCompletion(scene: hyperScene)
        });
    })

}

my scenes init

init(config: HyperSceneConfig) {


    super.init(size: config.size, viewController: config.viewController)

    self.backgroundColor = SKColor.blackColor()

    self.levelTime = 200

    // this is where i do all my setup before the scene is presented
    self.setupScene()
}

this is how I present my scene in my gameViewController.

You could do the same sort of thing in a scene to load the next level. This is great because you can create a seamless transition between levels since everything is loading on a different thread.

HyperScene.createResources(hyperConfig, withCompletion: {
    scene in
    self.scene = scene
    self.scene.scaleMode = .AspectFill
    self.skView.presentScene(self.scene)
    self.view.addSubview(self.skView)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM