Skip to main content
Inspiring
April 27, 2010
Answered

How do I get external classes to work in AS3? Sample included.

  • April 27, 2010
  • 1 reply
  • 2514 views

I'm trying to make this work, and something basic must be missing.  Its straight out of the Adobe help file example.

CS4, Actionscript 3

Here's what I'm doing:

1.  create an .as file and name it "HelloWorldExample"

2.  Put the script below in it and save it.

3.create a new .fla file and save it to the same directory.

4.in that .fla file, put "HelloWorldExample" in the Document properties Class field

5. Save it and run the movie.

I get a white screen.  Nothing.  I've tried a few other examples as well, but I haven't been able to get ANY external .as files to run. This is the simplest one.

Do I need to import them or call them in some way other than step 4?

Did I miss something?

package

{

    import flash.text.engine.*;

    import flash.display.Sprite;

    public class HelloWorldExample extends Sprite

    {

        public function HelloWorldExample()

        {

            var str = "Hello World! This is Flash Text Engine!";

            var format:ElementFormat = new ElementFormat();

            var textElement:TextElement = new TextElement(str, format);

            var textBlock:TextBlock = new TextBlock();

            textBlock.content = textElement;

            var textLine1:TextLine = textBlock.createTextLine(null, 300);

            addChild(textLine1);

            textLine1.x = 30;

            textLine1.y = 30;

        }

    }

}

This topic has been closed for replies.
Correct answer

As long as you have your class paths set properly, it's better to have your .as files located in a different loc than your .fla. What if you want to use the same class in different .fla files? Are you going to copy it into every project folder? That sort of defeats the purpose of using classes then, because if you change it you have to change it in various places. Not good. Not OOD.

So: Edit > Preferences > AS3 Settings - Add paths to the source path... or one path... something like c:\flashclasses\as3

Then inside your as3 folder you set your package folders. If you use TweenLite you'd have:

com

     greensock

          TweenLite classes

or your own:

com

     myDomain

          my classes

then you'd import like:

import com.greensock.TweenLite;

import com.myDomain.myClass;

Make sense?

1 reply

McFriscoAuthor
Inspiring
April 27, 2010

I figured it out. 

Another one of those "I'm a dummy" things. 

The .as file and the .fla file were in different directories.

But it bears mentioning as it could hang other people up.

To see how this can trip you up, create a new .AS file, and save it, where ever the file save dialog box points you.

Then create a new .fla file and save it where ever the file save dialog box points you.

Its NOT necessarily the same directory as the .as file, even though it seems to me it should be...

Hopefully this will help somebody keep their sanity.

Correct answer
April 27, 2010

As long as you have your class paths set properly, it's better to have your .as files located in a different loc than your .fla. What if you want to use the same class in different .fla files? Are you going to copy it into every project folder? That sort of defeats the purpose of using classes then, because if you change it you have to change it in various places. Not good. Not OOD.

So: Edit > Preferences > AS3 Settings - Add paths to the source path... or one path... something like c:\flashclasses\as3

Then inside your as3 folder you set your package folders. If you use TweenLite you'd have:

com

     greensock

          TweenLite classes

or your own:

com

     myDomain

          my classes

then you'd import like:

import com.greensock.TweenLite;

import com.myDomain.myClass;

Make sense?

McFriscoAuthor
Inspiring
April 28, 2010

Good point.

Now this brings up another problem that I'm having with external classes.

I tried to implement this package called "AccessibilityPropertiesExample" from the Adobe help files.   here's a link :

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/accessibility/AccessibilityProperties.html

When I try to run it, I get an error that says "5006: An ActionScript file can not have more than one externally visible definition: CustomAccessibleButton, CustomSimpleButton" 

There are 2 additional classes in the package.  I guess this is what breaks it.  I tried importing the classes individually in the fla file, but that didn't work.

How do I run packages with more than one class?

">McFrisco