Saturday, 5 January 2013

Access movie clip with SWF Loader

In one of my older post I wrote about loading external swf’s using the Greensock SWFLoader. Today I will post about accessing movie clips dynamically from externally loaded swf’s.

My example below assumes the external swf has a movie clip in the library with the ‘Export to Actionscript 3’ enabled and the Class name: MyMovieClip. The external swf has no content on the stage.

import com.greensock.loading.SWFLoader;
import com.greensock.events.LoaderEvent;


var loader:SWFLoader = new SWFLoader('example.swf',{name:'loader', onComplete:onCompleteHandler});
loader.load();


function onCompleteHandler(e:LoaderEvent):void
{
 var MyMovieClip:Class = loader.getClass('MyMovieClip');

 var myMc:MovieClip = new MyMovieClip();
 addChild(myMc);
}

From the code above I have loaded an external swf called example.swf. In the onComplete function I have used the getClass() method to get a reference to the class by name, and added the external movie clip on the stage.

To access externally loaded movie clips with native Actionscript 3 code. You can use the following code. I have used the applicationDomain.getDefinition() method to get a reference to the class by name.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
loader.load(new URLRequest('example.swf'));


function loaded(e:Event):void
{
 var MyMovieClip:Class = e.target.loader.contentLoaderInfo.applicationDomain.getDefinition('MyMovieClip') as Class;
  
 var myMc:MovieClip = new MyMovieClip();
 addChild(myMc); 
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP