Solved

AS3 - how to refer to a dynamically added child swf

Posted on 2008-10-21
4
1,182 Views
Last Modified: 2013-11-11
This has got to be so easy but I can't figure it out. In AS2, if you wanted to add a child swf to a parent, you'd create an empty movieclip and then load the swf into the movieclip. Then if you wanted to refer to the swf, you'd refer to the movieclip. For example,

this.childMovie._x = 20;

OK, so I'm working on an AS3 project now and I've loaded a child swf with the loader and I'm displaying it with addChild. Everything's great, except now I want to add some children to the child swf and I don't know what to call it! Is it's name the loader name? Is it stored there, sorta so that to refer to it, I'd say

swfLoader.addChild(newObj)

I'm just not sure and I'm getting some errors so I don't know if that means that I'm doing it wrong or if I just have an error somewhere else that I need to fix.
0
Comment
Question by:katyjack
  • 2
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 22773266
to refer the movieClip, use "Loader.content"


for example, if you Loader instance name is "myLoader" then following will refer the child SWF

myLoader.content
0
 

Author Comment

by:katyjack
ID: 22775229
For example?

swfLoader.content.addChild(optionBtn);

When I do that, I get the error "Call to possibly undefined method addChild"
0
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 22776276
You have to wait for loading completion before accessing loading SWF...

here is the sample code for your clarification:


		var swfLoader:Loader = new Loader();

		swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);

		var _request:URLRequest = new URLRequest("external.swf");

		swfLoader.load(_request);

		addChild(swfLoader);

		

		

		function onComplete(ev:Event):void {

			var loader:LoaderInfo = LoaderInfo(ev.target);

			var mc = loader.content;

			// you may access external loaded SWF here..

			mc.addChild(optionBtn);

		}

Open in new window

0
 

Author Closing Comment

by:katyjack
ID: 31508600
You're exactly right - I had an onComplete function but I had the code to call the swf in another file and it was firing before the swf was loaded. Thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now