0

I'm programming an AS3 application which loads an external SWF file to a movie clip in my stage. I need to read a variable inside the embedded SWF. I think I can, probably, do this through the bgURL, but I can't figure out how.

How can I read a variable inside the embedded SWF?

var bgLoader:Loader = new Loader();

var bgURL:URLRequest = new URLRequest("file.swf");
bgLoader.load(bgURL);
addChild(bgLoader);
4
  • Depends on where your variable is relative to file.swf. For example if there's a variable myVar at the top level of file.swf (think of it as global), you should be able to do something like: trace(MovieClip(bgLoader.content).myVar); after the swf has completely loaded Commented Aug 14, 2011 at 23:47
  • It's actually a few layers deep. If the loaded MovieClip was named "top", here how I'd access it: trace(top._root.Player.played); I don't quite understand what you did with the MovieClip, what does "MovieClip(obj).varName" mean? Commented Aug 15, 2011 at 0:02
  • is the external swf also AS3? Commented Aug 15, 2011 at 10:28
  • Just FYI: your question was mentioned on meta with regard to a rejected edit. That is the reason it is experiencing a bit more attention than would normally be the case. Commented Nov 6, 2016 at 15:27

1 Answer 1

1

That would be something like trace(MovieClip(bgLoader.content).Player.played);, but make sure you access the content in the Event.COMPLETE handler:

bgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,bgLoaded);
function bgLoaded(event:Event):void{ trace(MovieClip(bgLoader.content).Player.played) }

bgLoader.content returns a DisplayObject, but you need to access you content as a MovieClip. To do so you use casting.

This is presuming your external swf is also AS3 (good point Teo.sk !) This bit: _root.Player.played looks like AS2. Unfortunately you can't access variables form a loaded AS2 movie directly. Still, you can use Local Connection class to send variables back and forth betwen AS2 and AS3. Luckily Grant Skinner wrote a nice little utility called SWFBridge to make this easier

Sign up to request clarification or add additional context in comments.

2 Comments

I just found out the embedded swf is AS2. are you sure I can't access the variables? because, when I embedded the same swf in my C# Windows Application, I could access its variables.
I am pretty sure you can't access the as2 variables from an as3 swf which loads it. The as2 swf runs as an AVM1Movie instance in as3 which is restricted in order to run AVM1(as1 and 2 swfs) and AVM2(as3 swfs) side by side. I had no clue you can access variables from a C# Application, that's pretty cool.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.