Search

Rss Posts

Rss Comments

Login

 

Calling Methods in the Timeline in AS 3.0 - Flash CS3

Feb 16

When first transitioning to Actionscript 3.0 and the new Flash CS3 O-O model, a lot of things became cleaner and easier to accomplish with code. One notable exception to that however, was calling methods in a timeline, specifically those in an externally loaded .SWF.

The short answer to this is: when calling a method in this scenario, the external SWF must be cast as a MovieClip prior to interaction occurring. I know that sounds strange- “cast a MovieClip as a MovieClip ..??” -but with the new 3.0 object hierarchy, there is a reason:

To grab an external Movie (.SWF), you’d probably use a loader (it’s the cleanest way IMHO):

public var swfMovie:Loader = new Loader();
swfMovie.load(new URLRequest(”movieclips/mc1.swf”));

Once this happens, your main (parent) clip sees this .SWF as a Loader’s content. So it looks at the clip as essentially a child object contained within the instance of the swfMovie loader - specifically it’s .content property.

var externalSWF = MovieClip(swfMovie.content);
externalSWF.startMe();

So to cast it, use the syntax illustrated to create a new MovieClip (you may have to import the MovieClip class def.) variable, and cast the swfMovie’s content property. From there just call the method!

As a side note: the external movieclip’s playhead must be on the frame(s) with the method you’re calling to function properly. E.g. this external .SWF was stopped in the first frame, which also had the startMe() definition in it.

Post a comment