Correct method of incorporating Starling with Preloader

993 Views Asked by At

Hi would like to know the best method of adding a preloader. Then when the content is loaded create an instance of Starling framework. Here is what I have:

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import loaders.Preloader;
import starling.core.Starling;    

//main class container
 public class Game extends Sprite 
{
    //preloader class
    private var _loader:Preloader;

    //starling instance
    private var _starling:Starling;

    //constructor to initialise game
    public function Game():void 
    {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage)
    }

    private function onAddedToStage(e:Event):void 
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        _loader = new Preloader();
        addChild(_loader);

        _loader.addEventListener("loaded_game", onGameLoaded);
    }

    private function onGameLoaded(e:Event):void 
    {
        trace("game loaded");
        _starling = new Starling(Game,stage,null,null,"auto","baseline");
        _starling.start();
    }

}

This is the Preloader class:

package loaders
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class Preloader extends Sprite
{
    private var textLoaded:TextField;

    public function Preloader()
    {
        this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void
    {
        // remove added to stage listener
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        textLoaded = new TextField();
        textLoaded.x = 300;
        textLoaded.y = 300;
        textLoaded.border = true;
        textLoaded.autoSize = "center";
        addChild(textLoaded);

        //loop current load state
        this.addEventListener(Event.ENTER_FRAME, loadGame);
        this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
    }

    private function onRemovedFromStage(event:Event):void {
        this.removeEventListener(Event.ENTER_FRAME, loadGame);
        this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
    }

    private function loadGame(e:Event):void
    {
        //vars
        var total:Number = parent.stage.loaderInfo.bytesTotal;
        var loaded:Number = parent.stage.loaderInfo.bytesLoaded;

        textLoaded.text = Math.floor((loaded/total)* 100) + "%";

        if (total == loaded)
        {
            this.removeEventListener(Event.ENTER_FRAME, loadGame);
            dispatchEvent(new Event("loaded_game", true));
        }
    }

}//end of class

}

Is this the best way of doing it or is there a more efficient, practical option? As you can see I am making use of Flash classes before I instantiate Starling? The Preloader is simply a textfield listing what is loaded, this should allow the application to load up faster.

1

There are 1 best solutions below

0
On

Don't you think that it would make more sense to create an external SWF that preloads your game? It is always a better solution than incorporating a preloader into your main game file.