Jump to content
Search Community

Event dispatch and listen through LoaderMax

carlosfandang test
Moderator Tag

Recommended Posts

Hi, I'm getting stuck with something which I'm sure is dead simple - event listening and dispatch. Find it hard anyway, let alone through using it in LoaderMax.

 

Basically, I'm using LoaderMax to load separate SWF's ("EventsAlbum", etc) into a parent SWF ("Gallery Holder"). That's cool, got that and very happy there. But my problem is that the child swf's are SlideShowPro galleries which have a delay in starting up in their AS3 component (so you get a blank screen for a while - it's a recognised problem with their component!!).

 

So I thought, I could get the parent to listen for the "onAlbumStart" event that you can script in the child (i.e when the gallery has completed its load) and then run a TweenMax. BUT I'm just not understanding the event bubbling process at all !!

 

So I have a LoaderMax set up and then a btn function load it:

var queue1:LoaderMax=new LoaderMax({name:"photoQueue1"});
queue1.append( new SWFLoader("photos/events.swf", {name:"EventsAlbum", estimatedBytes:96000, visible:false, container:this, x:44, y:47}) );
queue1.load();
var contentevents:ContentDisplay = LoaderMax.getContent("EventsAlbum");

function eventslaunch(event:MouseEvent):void {
// set loader content and visibility
	LoaderMax.getContent("EventsAlbum").visible = true;	

 

 

The child script is below - this says when the Album has loaded and started:

eventsgallery.addEventListener(SSPAlbumEvent.ALBUM_START, onAlbumStart);

// Preloading is done, what to do now?
function onAlbumStart(eventObject):void  {
  trace("GALLERY LOADED");
};

 

In the parent, I simply want to know when "onAlbumStart" has happened and then run a simple Tween. Can anybody help a dumbass with the script to dispatch event in the child and then listen through a LoaderMax in the parent?

 

Sorry, I'm always the stupid one, but I'm learning!!

Link to comment
Share on other sites

In your child, you could dispatch a custom event like:

 

eventsgallery.addEventListener(SSPAlbumEvent.ALBUM_START, onAlbumStart);

function onAlbumStart(eventObject):void  {
  trace("GALLERY LOADED");
  dispatchEvent(new Event("galleryLoaded"));
}

 

Then in the parent, you'd do something like:

 

var loader:SWFLoader = new SWFLoader("photos/events.swf", {name:"EventsAlbum", estimatedBytes:96000, visible:false, container:this, x:44, y:47, onInit:initHandler}) );
loader.load();
function initHandler(event:LoaderEvent):void {
   loader.rawContent.addEventListener("galleryLoaded", galleryLoadedHandler);
}

function galleryLoadedHandler(event:Event):void {
   trace("gallery loaded! (notified parent)");
}

 

(note: the rawContent property of an SWFLoader refers to the actual swf's root whereas the "content" refers to the ContentDisplay container into which the swf was placed)

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hi,

 

I think I've basically go to grips with loadermax, but wonder if you could give me an idea ond a silly problem that I'll try and explain but is still related to the 3rd party dispatchevent!!

 

Basically, I animate my main site at load (I keep the load small initially for many reasons, including stuttering from 3rd party apps loading during animation). Then I load swfs when I need them (i.e the .load() event only occurs when a menu button is clicked). However, it seems crazy not to actually have some of these sub swf's loading in the background, BUT I have a newbie problem concerning "if" a sub swf is loaded or "else" (but not relating to your loadermax oncomplete).

 

My main issue is that most of my sub swf's are built through third party apps, they are quite large galleries and therefore I would like to load them once and then just play with visibility, etc. Makes sense - I load a sub swf, a "loading" icon is displayed until the third party app dispatches it's own complete event at the end of a load (as this post originally dealt with). That's cool - show icon, then hide it on 3rd party dispatchevent. Then I hide the window when other menus are clicked.

 

My problem is that if I then click on that menu again (say to toggle the visibility of the app) - then the "loading" icon is displayed again, but as the 3rd party app is already loaded it will not disappear as there is no dispatchevent again from the 3rd party app. Whilst, I've done a "hoaky" fix (replace the original menu button on first click with another that doesn't launch the "loading" code) - is there a way to code some script to say if "3rd party dispatchevent has already happened "don't show loading layer again".

This also affects why I am not loading it in the background - if a user clicks the menu and it hasn't finished loading, then that's fine, but if it has finished then the "loading" icon is shown and won't disappear - again I could do a "hoaky fix" but surely there is a simpler way!!

 

Sorry for longwinded explanation, but hope that makes sense. Thanks in advance.

Link to comment
Share on other sites

Yeah, it's super simple if you're just using LoaderMax to load all that stuff because you could check any loader's "status" or see if the "progress" is 1 but if you've got custom stuff that dispatches custom events, blah, blah, blah, then my recommendation would be to create a small set of wrapper classes that you use to load the various types of content. Maybe you'd have a base AssetLoader class that has a "loaded" Boolean property. You can leverage LoaderMax stuff inside the class, but for your custom event functionality, you'd manually toggle that "loaded" value. Kinda like...

public class AssetLoader {
public var loaded:Boolean;
public var loader:LoaderCore; //a SWFLoader, ImageLoader, LoaderMax, whatever.

public function AssetLoader() {
	// constructor code
}

public function load() {
	this.loader.load();
}
}

 

Then you extend that class for each type of 3rd party specialized app you've got, like...

 

public class SlideShowProLoader extends AssetLoader {

public function SlideShowProLoader(url:String, vars:Object=null) {
	this.loader = new SWFLoader(url, vars);
	this.loader.addEventListener(LoaderEvent.COMPLETE, _completeHandler);
}

private function _completeHandler(event:LoaderEvent):void {
	SWFLoader(this.loader).rawContent.addEventListener("galleryLoaded", _galleryLoadedHandler);
}

private function _galleryLoadedHandler(event:Event):void {
	this.loaded = true;
}

}

 

Of course you could/should refine that code a bit, but I tried to keep it simple to demonstrate the concept.

 

Then you could simply check your AssetLoader's "loaded" property to see when it's truly loaded. You can still add stuff to a LoaderMax queue because we exposed a "loader" property that's a LoaderCore (which could be appended to any LoaderMax, loaded on its own, get the content if you need it, etc.) So it has all the LoaderMax goodness while wrapping your custom functionality into a class the way you need it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...