Jump to content
GreenSock

dathenous

Problem with LoaderMax?

Recommended Posts

This is my first project with LoaderMax, and I believe I uncovered a bug. This bug does not occur when loading the project from within the Flash dev environment. It only shows when loading files from a server. I've boiled the code down to the bare minimum so you can see the bug, plus I've linked to my source.

 

Here's my set-up

Mac OS X 10.6

Safari 5.0

Flash Player 10.0 r45

Flash CS4

Code written in AS3

 

Here's the synopsis:

 

I have three files, Main.swf, letter-a.swf and letter-b.swf. Main.swf contains two LoaderMax instances that load letter-a.swf and letter-b.swf respectively. I first built this demo without Class names defined on the letter swfs, using that set-up everything worked fine. Then, I added the Class name "Main" to each of the letter swfs, that's when the bug shows. You won't see it when loading the project from the Flash dev environment, but it shows when you upload to a server and load remotely.

 

You can see the error in action here: http://www.danmyers.name/loadermax-test/index.html

 

You can download the entire project from http://www.danmyers.name/loadermax-test/error-demo.zip

 

Here are my class definitions:

 

Main.swf

package {
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import com.greensock.plugins.*;

import flash.display.*;
import flash.text.*;

public class Main extends MovieClip {

	private var _loader1:SWFLoader;
	private var _loader2:SWFLoader;

	public function Main():void {
		_init();
	}

	private function _init():void {

		//create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners
		var queue1:LoaderMax = new LoaderMax({name:"queue1", maxConnections:1, auditSize:false, integrateProgress:false});
		var queue2:LoaderMax = new LoaderMax({name:"queue2", maxConnections:1, auditSize:false, integrateProgress:false});
		 

		//var patha:String = "a/letter-a.swf";
		var patha:String = "http://www.danmyers.name/loadermax-test/letter-a.swf";
		_loader1 = new SWFLoader(patha, {name:"letter-a", estimatedBytes:10,  x:0, y:0, autoPlay:true});
		queue1.append( _loader1 );
		addChild(_loader1.content);

		//var pathb:String = "b/letter-b.swf";
		var pathb:String = "http://www.danmyers.name/loadermax-test/letter-b.swf";
		_loader2 = new SWFLoader(pathb, {name:"letter-b", estimatedBytes:10,  x:250, y:0, autoPlay:true});
		queue2.append( _loader2 );
		addChild(_loader2.content);

		//start loading
		queue1.load();
		queue2.load();
	}
}
}

 

letter-a.swf & letter-b.swf (Both use this same code)

package {
import flash.display.MovieClip;
public class Main extends MovieClip {
	public function Main():void {

	}
}
}

  • Like 1
Link to comment
Share on other sites

One more thing. All I have to do in order to fix the problem is rename the Class name on letter-a.swf. So instead of a Class named "Main" use "MainA". The problem goes away completely. I think that LoaderMax must be instantiating the classes by name, and therefore when there are two SWFs that have Classes with the same name, it causes this problem. The odd thing is, in the Flash dev environment, everything works great.

  • Like 1
Link to comment
Share on other sites

This isn't a bug actually - it has to do with the fact that you're using the same package and class name for the root of each swf and by default, LoaderMax uses a LoaderContext that grants you full script access by using the same ApplicationDomain and SecurityDomain (ApplicationDomain.currentDomain and SecurityDomain.currentDomain). Doing so ensures that even swfs loaded across domains will be able to be accessed with your code and BitmapData.draw() works, etc. (lots of people run into major headaches with this stuff otherwise, so LoaderMax is attempting to eradicate the common issues).

 

When ApplicationDomain.currentDomain is used, it basically means that the classes that are defined in your subloaded swf are merged into the same definitions list as the parent swf, and if the parent already has a definition of the same package and name, it will be used instead of the child's. Please read up on LoaderContext and ApplicationDomain:

 

http://www.adobe.com/livedocs/flash/9.0 ... ntext.html

http://www.adobe.com/livedocs/flash/9.0 ... omain.html

 

You can easily control the LoaderContext using the "context" special property in LoaderMax. So in your case, all you'd need to do is add this:

 

context:new LoaderContext(true, new ApplicationDomain())

 

Like this:

_loader1 = new SWFLoader(patha, {name:"letter-a", estimatedBytes:10,  x:0, y:0, autoPlay:true, context:new LoaderContext(true, new ApplicationDomain())});

 

That forces the subloaded swfs to use a new ApplicationDomain, segregating their class definitions from the rest of your swf.

 

Another solution would be to put your Main classes in packages of their own, like lettera.Main and letterb.Main or obviously make their class names different (as you already noticed). This is generally considered a best-practice anyway - creating classes with no package is highly discouraged.

 

By the way, thank you SO much for creating that simplified example set of FLAs! That was tremendously helpful and I wish every poster in the forums was as thoughtful! More often than not, the developer will just claim there's a bug or problem but won't provide any example or way for me to replicate the "bug". Or they'll provide an FLA that has 2000 lines of irrelevant code that they expect me to sift through. Yours was an ideal example, so thanks again.

  • Like 1
Link to comment
Share on other sites

Boiling down the code to a simple example is the very least I can do. I'd gladly exchange an hour of my time if it meant saving just a minute of yours.

 

I knew there had to be a simple explanation. I understand the namespace issues, but do you know why doesn't that problem rear it's ugly head in the Flash Dev environment? There have been a few cases where I've had problems with a Flash project only after I published and uploaded it. That seems to be a problem with Flash itself.

 

Anyway, thank you for your answer. And more importantly, thank you for your awesome work. I've always considered GreenSock to be the best of the best in Flash middleware, and LoaderMax is no exception.

Link to comment
Share on other sites

...do you know why doesn't that problem rear it's ugly head in the Flash Dev environment? There have been a few cases where I've had problems with a Flash project only after I published and uploaded it. That seems to be a problem with Flash itself.

 

There are indeed several problems with Flash itself, but the reason you didn't notice this locally is because Flash will throw errors if you try to set a LoaderContext that uses SecurityDomain.currentDomain and the swf is run locally. Weird, I know. Pretty frustrating actually. So LoaderMax has code internally that senses whether or not the file is running locally and makes the necessary adjustments.

 

Glad to have you on board and using the code, dathenous. Enjoy.

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.
×