Calling an External Class in AS 3

February 4, 2008 by jared 

This is how you call an external .as file (which is essentially a class).

1.) Download the sample files.

callextclass.zip

2.) Brief

You should see 3 files

  • CallingExtClass.fla
  • MainClass.as
  • AnotherClass.as

Open CallingExtClass.fla, and compile it to swf by pressing CTRL + Enter.

You should see this in the Output window

image

this is main doc Class” is being executed from MainClass.as

and “this is from the AnotherClass.as file” is from, well, AnotherClass.as.

3.) How does it work?

MainClass.as is the document class for CallingExtClass.fla

Open MainClass.as, and observe that the following piece of code in the constructor creates a new instance of AnotherClass.as

var _anotherClass:AnotherClass = new AnotherClass();

This simply means an object called _anotherClass has been created (loaded into the memory) but not yet placed on the stage. To do that we need the following line of code.

addChild(_anotherClass)

Think of the stage as a Parent object. Anything that is added to the stage will naturally be known as a Child object, hence, addChild(name_of_the object).

That’s how you reference / call an external class and load it onto the stage.

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!