Actionscript 3.0 - how do you work with multiple external class files?

Just starting out with moving from AS 2.0 to AS 3.0. Also posting to more specialized message boards, but for something like this methinks I’ll get a faster, better answer here :).

Let’s say we have a base class, Polygonal. Another class, Triangular, extends Polygonal. Polygonal would ultimately be extended by many other classes, such as Rectangular and Octagonal/ I want them each separate files. Here’s where I’m at:

  • If Polygonal exists on its own, but nothing else does, everything seems to work. Won’t do much, but at least it’ll do something.
  • Throw Triangular in the same directory, and give it the same package name. I would expect this to cause problems, but tried it anyway. What does happen, though, is a bit of a surprise: absolutely nothing. No error messages, but no function calls to anything, trace, seem to work.
  • OK, so put Triangular in a sub directory and name the package accordingly. But: Packages cannot be nested.
  • Fine. Leave out the package declaration. But any public class… must be in a package.

Feels a bit like one of those transit card vending machines that are currently only accepting cash - but no coins - and requires exact change when the fare is $2.25 per ride. Or (I’m hoping) there’s a headsmacker I’m missing here.

Anyhow, a simple what-the-heck-do-you-do would help a lot. Or better still, a link to a good tutorial on AS 3.0 external class files that doesn’t assume an engineering degree and also doesn’t leave you hanging after showing you how to instantiate one class on the stage.

I don’t really understand what the problem is, but anyway:

  1. In AS3 all classes MUST be in their own file, named after the class, in the directory tree conforming to the package. Say I’ve got a file containing org.mystuff.MyThingy:

package org.mystuff {
public class MyThingy {
 // methods and properties here
}
}

That should go in [some directory in your load path]/org/mystuff/MyThingy.as You can have as many classes in the same package as you like and they all should go in seperate files in the same directory.

  1. Packages can be nested, but package declarations possibly may not (I’m not sure).

IOW: you can have a package org.mystuff.extra and all classes in that would go in the /org/mystuff/extra directory. Each of those files should contain a full package declaration.

  1. AFAIK if your code does not actually use those classes (importing them is not enough), they won’t be compiled, and it’s possible that that’s the reason you can’t trace anything. Add a simple declaration in your top-level code/class that instantiates an object and see if that helps:

import org.mystuff.*;
var thingy:MyThingy = new MyThingy();


Though I do own a copy of Flash CS3, I do all my AS3 development with the flex compiler kit so I probably can’t help you with any issues you may have using the Flash IDE.

Thanks!

It turns out that I had some classpath settings that were really throwing a wrench in things. Took those out, and now at least I can get it to recognize files in the same folder.

Now it’s on to tearing my hair out figuring out why it won’t recognize subfolders. Ah, the forgotten joys of embarking on a new language…

I believe the subfolder name must be part of the package declaration, and it’s case sensitive. So if the structure is



org
 +- mystuff
   +- util


then package at the top of any files in util should be



package org.mystuff.util
...


and you have to import it as



import org.mystuff.util.*


import.org.mystuff.* does not include subfolders, if I remember rightly

Correct. All this stuff is pretty much directly ported from Java, and as far as I can see is mostly there to make it easier to create efficient compilers and static analysis tools. Not that efficient compilers and analysis tools are a bad thing, but I would really like a good way to selectively switch on/off the compiler warnings for some of the more useful, perfectly sensible constructs that AS3 allows but currently makes annoyingly hard to use.