PureMVC Architects Lounge

PureMVC Manifold => MultiCore Version => Topic started by: sventunus on February 28, 2011, 02:40:50

Title: When to initialize: in preloader or in main?
Post by: sventunus on February 28, 2011, 02:40:50
Dear community,

I have a question concerning a pure AS3 flash project with a preloader in frame 1.

I am wondering where and when I should initialize the PureMVC apparatus: in the preloader or in the main class that gets constructed once the preloader finishes it's job?
My gut feeling would say to start everything in the main class, registering the main view with the Facade through my applicationmediator and going on from there.

However, all the stuff that the preloader loads before launching the website has to remain available to the application. So maybe it would be better to call startup in the preloading stage (registering a mediator for the preloader and a proxy to do the actual preloading) and subsequently execute an initialize-macrocommand to initialize the whole website once the preloader has finished, or throw an error and destruct everything if preloading should fail.

What would be your recommendations?

Thanks in advance!

Title: Re: When to initialize: in preloader or in main?
Post by: sventunus on February 28, 2011, 10:05:34
Thought I might answer my own question here.

I initialized everything in the preloader, and this looks perfectly ok to me. Please let me know if you have any suggestions or remarks.
Hope this may help someone else who's in the same situation  :)

Preloader.as (irrelevant code omitted)
package com.thesedays.web2011
// import statements

* @author Sven Dens - These Days
public class Preloader extends MovieClip
// events constants
public static const STORE_CONFIGURATION:String = "event/store/configuration";
public static const BACKGROUND_SET:String = "event/background/set";

// Get a facade instance for this application
    private var _facade:ApplicationFacade = ApplicationFacade.getInstance( Web2011.NAME );
    // main application class
private var _main:Class;

// application class instance
private var _app:Object;

// stage assets for preloading
private var _loadingAnimation:TDLoader;
private var _background:Bitmap;

// preloader byte counters
private var _swfPercentLoaded:Number;
private var _backgroundPercentLoaded:Number;
private var _dataPercentLoaded:Number;
private var _totalProgress:Number;
    // the configuration that was received from the flashvars (or default config)
    private var _config:ConfigurationVO;
    // greensock Imageloader for the background image used in the preloader
    private var _backgroundLoader:ImageLoader;
    public function Preloader()
    // stop the timeline

// initialize the byte counters
_swfPercentLoaded = 0;
_backgroundPercentLoaded = 0;
_dataPercentLoaded = 0;
_totalProgress = 0;

// start the PureMVC apparatus

// set scale mode
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

// activate plugins

// load policies and set permissions for YouTube

// load the fonts

// initialize application settings

// listen for enter frame
addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);

// listen for resize
stage.addEventListener(Event.RESIZE, onResize, false, 0, true);

private function onLoadingAnimationFinished(evt:Event) : void
// remove event listener
_loadingAnimation.removeEventListener(TDLoader.ANIMATION_FINISHED, onLoadingAnimationFinished);

// advance to frame 2

// fade out the preloader and render the website

private function renderWebsite():void
_main = Class(getDefinitionByName("com.thesedays.web2011.Web2011"));

if(!_main) throw new Error("Could not retrieve the main application class com.thesedays.web2011.Web2011. Check your code!");

// instantiate the website
_app = new _main();
addChild(_app as DisplayObject);

// and call the startup method

// now clean up the preloader stuff
var i:int = numChildren;
while(--i > -1)
if(getChildAt(i) == _loadingAnimation)
_loadingAnimation = null;

i = NaN;
_swfPercentLoaded = NaN;
_backgroundPercentLoaded = NaN;
_dataPercentLoaded = NaN;
_totalProgress = NaN;
    _config = null;

package com.thesedays.web2011.controller
//import statements

* @author Sven Dens - These Days
public class StartupCommand extends SimpleCommand
override public function execute ( note:INotification ) : void
var preloader:Preloader = note.getBody() as Preloader;
if(!preloader) throw new ArgumentError("The object passed to the startup method could not be cast as a Preloader, check your code!");

// register a mediator for the Preloader
facade.registerMediator(new PreloaderMediator(preloader));

// register a proxy to store the configuration that was provided with flashvars
facade.registerProxy(new ConfigurationProxy());

// register a proxy to store the application's data that will be preloaded
facade.registerProxy(new ApplicationDataProxy());

// and register a proxy to store the backgrounds that will be loaded from the server
facade.registerProxy(new BackgroundsProxy());


And then the main class, Web2011.as
package com.thesedays.web2011
import flash.display.MovieClip;

* @author Sven Dens - These Days
public class Web2011 extends MovieClip
// App name
    public static const NAME:String = "Web2011";
public function Web2011()


public function startup():void
trace("application started");

Title: Re: When to initialize: in preloader or in main?
Post by: puremvc on March 10, 2011, 09:56:06
Normally I build Flex or AIR apps, so initializing PureMVC in the preloader doesn't make sense, because the initial display hierarchy hasn't been built yet. However in the view preparation phase of PureMVC startup I usually want to wrap mediators around existing components in the display list.

Also, view preparation should come after model preparation, since the mediators generally want to retrieve references to the proxies they collaborate with in their onRegister handlers.

I think this process still makes sense in an AS3-only application, it's just that your view preparation phase will include creating the display list as well as mediating it.

What do you gain by doing this in the preloader? I can tell you what you lose: the preloader can't be reused another application now, because it has a dependency on the ApplicationFacade for this one application.


Title: Re: When to initialize: in preloader or in main?
Post by: puremvc on March 10, 2011, 10:17:04
Another question I'd ask is why the preloader needs to do loading of all these assets?

The role typically served by the preloader is to give you a way to have something happening visually while the rest of the SWF loads. After that, your app should take over. By putting all this asset management into the preloader, you're really overloading it (no pun intended) with responsibilities better left to the application during an ordinary PureMVC startup process.

The simpler the preloader, the more reusable it is. Here are some examples: