Jump to content
Search Community

LoaderMax beta: access to the content of loaded SWF

fixedmachine test
Moderator Tag

Recommended Posts

I don't know if I'm doing something wrong, but I can't get access to the content from loaded SWF. This should be obvious task. I'm using onInit event so everything should be fine.

 

Here is my simple code:

package  {
import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.loading.*;
import com.greensock.plugins.*;

import fl.events.*;

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

import nl.demonsters.debugger.MonsterDebugger;

public class Main extends MovieClip {

	public var _loader:LoaderMax;
	public var _swfLoader:SWFLoader;

	private var debugger:MonsterDebugger;

	public function Main() {
		debugger = new MonsterDebugger(this);
		_init();
	}

	private function _init():void {

		_loader = new LoaderMax();

		_swfLoader = new SWFLoader("contentSWF.swf", {name:"mc_content", container:this, x:0, y:0, onInit:_onInitHandler});

		_loader.append(_swfLoader);
		_loader.load(true);
	}

	private function _onInitHandler(event:Event) {
		trace(event.target.content.testRect);
	}
}

}

 

contentSWF.swf contains only one element on the stage - rectangle named testRect. I think that I should have access to it through content property, but I have an error:

ReferenceError: Error #1069: Property testRect not found on flash.display.Sprite and there is no default value.
at Main/_onInitHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.greensock.loading.core::CommonLoader/_initHandler()
at com.greensock.loading::SWFLoader/_initHandler()

 

I've got some weird listing in MonsterDebugger too. Sometimes it shows that there is nothing inside mc_content (sprite):

swfloader2.png

 

and sometimes that it contains my testRect but inside another instance MovieClip:

swfloader.png

 

Am I doing something wrong? Or it's a bug?

 

I'm using 2010-06-01 (version 0.81) of LoaderMax.

 

All my test files in the attachment.

Link to comment
Share on other sites

I can see why this would initially be confusing, but allow me to explain:

 

ImageLoaders' and SWFLoaders' "content" refers to a Sprite that's created immediately, and when your remote content (image or swf) is loaded, it is placed into that Sprite at index 0. This serves several purposes, the most significant of which is that it gives you something to work with right away (similar to Adobe's Loader class). For example, you may want to place objects on the stage for a slideshow and even allow the user to interact with them while the content is loading. So in your _onInitHandler(), you referenced event.target.content.testRect but that's not correct because event.target.content is that container Sprite, NOT your subloaded swf's root. But don't worry - I just posted an update (v0.91) that adds a "rawContent" property to ImageLoader and SWFLoader that will give you what you're after.

 

In any case, that "extra" object in the hierarchy you saw was the container Sprite.

 

One important thing to remember is that if your subloading swf runs into security sandbox issues (like if you're loading from another domain and there's no crossdomain.xml file in place), SWFLoader will automatically adjust the LoaderContext and fall back to a more restricted mode (instead of just burping a Security Error and stopping) and try loading again. Because of restrictions in the Flash Player, the swf in that case would be forced to stay inside its Loader object, so rawContent would return a Loader instead of your swf's root. Again, this only happens when there are security issues.

 

Long story short: download the latest version and then adjust your code to be:

 

OLD/BAD: trace(event.target.content.testRect);

NEW/GOOD: trace(event.target.rawContent.testRect);

 

By the way, thanks for the detailed post. You have no idea how valuable that is - way too often folks just complain that something isn't working but don't provide any example, screen shots, code, or anything to go on.

Link to comment
Share on other sites

Thanks! Now everything works just fine :)

 

But there is another issue, which was my primary issue I want to show you, but I've stuck on that content/rawContent problem :)

 

It's about Runtime Shared Libraries which are now popularized by the new Text Layout Framework added to CS5. I'll write another post about that problem.

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