1

I'm trying to listen for a custom event from an SWF I've loaded and I'm just not able to capture it. The loading code right now is just:

public function loadGame(gameSrc:String,gX:Number,gY:Number):void {
    var loader = new Loader();
    var addedDefinitions:LoaderContext = new LoaderContext();
    addedDefinitions.applicationDomain = new ApplicationDomain();   
    loader.load(new URLRequest(gameSrc));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

function onLoaded(evt:Event):void {   
    var game:MovieClip = MovieClip(evt.target.content);

    game.x = gX;
    game.y = gY;    

    chamber.mc_gameHolder.addChild(game);    
    Tweener.addTween(chamber.mc_gameTitle,{alpha:1,time:.75});  

    game.addEventListener("showQuiz",showQuiz);  
}

}

I know the event is being fired from my loaded SWF because I also have a listener in there that traces out a "hello" when it's fired.

Anyone? And apologies if this has been posted before - search didn't turn up anything specific.

7 Answers 7

2

I ran into the same problem. Here is what you need to do. When instantiating your LoaderContext, make sure the LoaderContext is using SecurityDomain.currentDomain. That will solve your problem.

1

This would work only if both SWFs are AVM2Movie (made using AS3), which I assume is the case here because otherwise casting to MovieClip would have thrown an error on run-time.

Are you sure that the event is dispatched by the document class of the loaded swf and not by one of its children? Because you are calling addEventListener on game which is the document class (root) of the loaded SWF and it won't catch events dispatched by its children. Can you show the code where you dispatch the event?

0

It may be possible that the event is being dispatched before the Event.COMPLETE event. Try adding a listener for the Event.INIT event. The Event.INIT event is dispatched when the Loader first has access to the loaded swf's document object.

0

hm, shouldn't it be :

addedDefintions.applicationDomain = ApplicationDomain.currentDomain to permit the loaded video to 'access' the parent one ?

Also for testing purposes I suggest to bubble the event up to make you haven't missed out a display object in between.

0

Things to consider:

  • The loaded clip should be from a security-enabled domain. If it's not the same domain the loading flash resides at, it should be included in the crossdomain.xml file. And loaded too.
  • Manually setting "allowDomain" via Security.allowDomain towards the loaded clip's domain is never bad.
  • The event should be bubbling, as Flash might add a layer or two of containers between the "game" var and the actual content.
  • Both loading and loaded clips MUST be AS3.
  • It is possible that the target clip is trying to load things from it's own proper location, so when you load it under a different URL, it can't find the files and fails, never reaching the event-firing phase in the first place.
0

Create a LoaderContext that sets the ApplicationDomain to the currentDomain, then pass it with your load() call:

loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleLoaded );
var loaderContext:LoaderContext = new LoaderContext(false, new ApplicationDomain( ApplicationDomain.currentDomain ) );
loader.load( new URLRequest("yoMama.swf"), loaderContext );

You can confirm that you don't have a crossdomain issue by having the loading swf trace a var or function call from the loaded swf. If you get the expected result, crossdomain.xml is not your issue.

1
  • new ApplicationDomain( ApplicationDomain.currentDomain ) == a new domain == cannot access code. ironic test... in a fresh project... check if the loader info (of the base swf class) app domain is the same as the current app domain == the are not! You have to use ApplicationDomain.currentDomain which only maps after base class is full constructed NOT during construction. Jan 24, 2013 at 20:36
0

You have to consider some things in your situation.

(1) if you are loading code from the SAME domain, the app domain is not necessary.

(2) if you are loading code form DIFFERENT domains, check the [crossdomain.xml] policy file first... second... if you dont want to waste time fixing this... use PHP and curl the file to your domain... then load that (Flash will think its loading it from the same domain).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.