R – Access MovieClips in one file from another SWF file

actionscript-3flash

i am new to Flash and AS.
I have to 2 swf file one in as2 and another in as3 , i have loaded swf file(as2) in as3 swf, its working but i need to access all the movieclip from(swf(as2)) and change the property(like style) dynamically . how can i do it .

Here my code::

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
addChild(loader);
loader.load(new URLRequest("games.swf"));
loader.x = 50;
loader.y = 125;

function handleComplete(event:Event):void{


    trace("swf loaded");
}

the above code is working fine and swf file is also loaded. how to access the individual movieclip from swf as2 file.. i know all the name(ID) of the movie clip also.

Help me to Overcome from this problem …Thanks in advance..

Best Answer

I recommend using the Event.INIT instead of the Event.COMPLETE. INIT gets trigger after the load is COMPLETE and the all the classes/instances in the loaded swf are initialized and ready to use.

var loader:Loader = new Loader(); 
loader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
addChild(loader);

function handleInit(e:Event):void{
var as2Movie:AVM1Movie = e.target.content as AVM1Movie;
}

You can access the loaded content through the content property of the Loader class. Not that since you're loading an as2 movie into an as3 movie, some restrictions apply:

"AVM1Movie is a simple class that represents AVM1 movie clips, which use ActionScript 1.0 or 2.0. (AVM1 is the ActionScript virtual machine used to run ActionScript 1.0 and 2.0. AVM2 is the ActionScript virtual machine used to run ActionScript 3.0.) When a Flash Player 8, or older, SWF file is loaded by a Loader object, an AVM1Movie object is created. The AVM1Movie object can use methods and properties inherited from the DisplayObject class (such as x, y, width, and so on). However, no interoperability (such as calling methods or using parameters) between the AVM1Movie object and AVM2 objects is allowed.

There are several restrictions on an AVM1 SWF file loaded by an AVM2 SWF file:

The loaded AVM1Movie object operates as a psuedo-root object for the AVM1 SWF file and all AVM1 SWF files loaded by it (as if the ActionScript 1.0 lockroot property were set to true). The AVM1 movie is always the top of any ActionScript 1.0 or 2.0 code execution in any children. The _root property for loaded children is always this AVM1 SWF file, unless the lockroot property is set in a loaded AVM1 SWF file. The AVM1 content cannot load files into levels. For example, it cannot load files by calling loadMovieNum("url", levelNum). The AVM1 SWF file that is loaded by an AVM2 SWF file cannot load another SWF file into this. That is, it cannot load another SWF file over itself. However, child Sprite objects, MovieClip objects, or other AVM1 SWF files loaded by this SWF file can load into this."From as3 docs.

For more info see the AMV1Movie Reference.

If you want to call as2 movies from as3 you will need to use Local Connection to establish communcation between the 2 swfs.

Grant Skinner wrote a handy thing called SWF Bridge for this kind of situation(easy as2 to as3 communication). It's worth a try.

Goodluck!

Related Topic