my gaming past, present and future
Tuesday February 7th 2012

Interesting Sites

Insider

Archives

ActionScript 3 Class Definition and Importing Sickness

Sorry, but this post is about some programming garbage.
Okay, so i decided to write a little ActionScript 3 code since it’s all object-oriented and somehow “real” compared to the previous joke versions. I was trying to create a small class that did some simple stuff and get it working in the Flash environment. After reading several examples straight from Adobe and some forums I wrote what looked like a simple, pristine piece of code that should have worked.

The error I got when I tried running the code was this:

1180: Call to a possibly
undefined method xxxx

This basically means that Flash couldn’t find the class that I created. But I didn’t get any errors from the “import” statement that references the .AS file that has the class in it (inside a package{}).

If I changed the filename I’m trying to import I get an error. Ok, so it knows the file is there but it cannot find the class inside the file for some reason.

I searched all over the net for an hour trying to find the answer to this problem. NOWHERE DID I FIND THE SOLUTION THAT I AM ABOUT TO GIVE YOU. The solution to the problem is something that, evidently, Flash programmers JUST KNOW from experience but the solution isn’t written anywhere that I could find on the internet and my Flash coders don’t know how they know the technicality I’m about to explain.

So it comes down to this: the way you write the name of your class MUST be the EXACT way you name the .AS file it’s in.

For example: if I create a class named BugJar like so:

package
{
  public class BugJar
  {
    //...code...
  }
}

then I **MUST** save it in a file named BugJar.as!!!!

Inside my framecode I would have:

import BugJar;

…and it would work.

This is just completely disgusting to me as a programmer. Linking the name of a class to the filename of the file it’s in is just wrong.

What’s worse is that Flash doesn’t even give you the proper error for this:

filename: bugjar.as

package
{
  public class BugJar
  {
      //...code...
  }
}

framecode:

import bugjar;
var jarnerdugJar = new BugJar();

The above example WILL NOT WORK and Flash doesn’t give you an error message that helps you understand or fix this problem that shouldn’t exist in the first place.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Reader Feedback

33 Responses to “ActionScript 3 Class Definition and Importing Sickness”

  1. megazoid says:

    Interesting post. Isn’t Java similar in that respect? The class name has to match the filename?

  2. romero says:

    Yes, Java is the same. Sick.

  3. forrest says:

    This is what happens when they make programming languages specially designed for quiche eaters.

  4. samuel nova says:

    If developing with Eclipse using Flex for pure ActionScript 3 code (Without having to learn Flash CS 3) then I’m pretty sure you will get an error.

    I thought it was ‘common’ knowledge more or less that class name and file name follows. Specially since you don’t specify what FILES to import but just what packages to import. ( So based on package/class name it knows what files to import )

    So if you look beyond the scope of 1 to 2 files of action script then it makes sense; also makes it a LOT easier to maintain code as you don’t have to look for what files the class is defined in. Like BugJar could be defined in “EnemyControlHandlingCode.as” winking

  5. haslo says:

    Even .net and the VisualStudio will by default have matching class and file names, although I don’t think it’s such a strict necessity there…

    Such a long rant about such a little thing seems a bit over the top to me happy

  6. eclipse says:

    that’s the same with Java, this interpreted programming languages are gays

    @samuel nova, something that limits you like that is never easier or better something that let you do what you want happy, simply because in a “normal” programming language you are able to name the file as you want in AS3 of Java but not viceversa!

  7. daniel klein says:

    Having to use matching class- and filenames totally makes sense (to me) and is used in a lot of modern programming languages. Remember that this is a MUST for late binding. How should the runtime system otherwise know where to look for a class at runtime?

    Also I don’t think that the problem in John example really is the naming convention. Isn’t it rather the compiler/runtime not throwing proper error messages?

    -Daniel

  8. duncan says:

    Found this by searching for “actionscript 3 package”
    http://www.adobe.com/devnet/actionscript/articles/actionscript_tips.html

    Under the bullet point for using Package declarations is a blurb about filenames that explains your error.

  9. romero says:

    First of all, i’m used to saving all my files in pure lowercase. I hate mixed case or using uppercase in programming. I’m a fan of lowercase and underscores.

    Typical class naming convention is that your classes should start with uppercase. This jacks up my file naming convention.

    @samuel nova: With AS3 you can only have ONE class per .AS file because of the way you *must* have the class and filename match.

    @haslo: I don’t like visual studio to generate a bunch of code and files for me. I prefer to keep it simple and by hand as much as possible. Luckily in VS i don’t ever have to name class files and classes the exact same.

    Guys, i’ve been coding for 29 years and this is the first time i’ve seen this stupid requirement. I am telling the compiler specifically which FILE i am importing in code and it should be able to scan that file and do any internal horseshit with associating the class name inside the file. That class name symbol should be loaded and when i use it elsewhere it should already know about it – and the name of the file should have nothing to do with it.

  10. fl0yd says:

    Even more bizarre is when you don’t define your class as public – you get the same error when you instantiate a new object but the import statement doesn’t fail. Why?!?!

    Coming from a Java background, AS 3 seems a blatant attempt to copy Java without delivering any of the benefits. Adobe’s language designers seem to be trying to fool us into thinking AS 3 is – like Java – a strongly typed language. The only hint it isn’t is when you get a strange error like the one you’ve posted.

    Java would have given you an error in the class definition, not in the calling class. That’s not ‘sick.’ And personally I don’t think there’s anything wrong with naming files/directories after the class/package name – this pattern sure makes it a lot easier to find the class file you are looking for. Sure the IDE could and should help with such things but the Flash IDE can’t even manage that! This kind of thing really makes you appreciate Java & Eclipse.

  11. forrest says:

    I wish I could hang out at your bad ass studio and code all day. =D

  12. alex says:

    That’s what you get for even glancing at garbage languages like AS or Java. ANSI C all the way baby happy

  13. rui ferreira says:

    I agree with all the above postings.

    This is one of those 95% things that you just don’t stumble upon in programming normally because they either:

    1) Are fixed by language and/or library long time ago
    2) Mature Compiler tools warn about these issues already
    3) Programmer’s way of doing things matches expectations of language and/or compiler and/or libraries.

    And this is the difference between 5 minutes of “Huh? What a hell?” and an eternal “One fucking lost hour, this is some stupid shit crappy coding crap.”

    Java coders would probably get that one on the 3rd level.

  14. anonymous says:

    as mentioned previously, class namefile name correspondence helps late binding for classes loading from disk at runtime, like with Reflection in Java, where the class is loaded by the jvm essentially by it’s filename.
    http://blogs.msdn.com/davidklinems/archive/2006/11/27/what-is-late-binding.aspx

    though I can understand the complaint happy

  15. romero says:

    Yes, i’m using multiple classes in one .as file but only one can be public.

    Not really that big of a deal for me. Just the file naming issue.

  16. anonymous says:

    This proves that Flash is for Gay Mac users who are also vegan. Pussies.

  17. qdqp says:

    Quote:
    I hate mixed case or using uppercase in programming. I’m a fan of lowercase and underscores.

    Although… camelcase function names can actually be really funny…

  18. yogarine says:

    Indeed, ActionScript is crap. But then again, so is Java. My all-time favorite programming language is PHP. winking

    I only realy program in AS in the first place because I really like Flex for creating RIA’s. Much less hassle and more forward compatible than AJAX, not to mention JavaScript is even worse than AS.

    I did read in the official Adobe documentation that filenames of Components/Packages/Modules need to be exactly the same name and case as the class name, so I never really have any issues with that.

    I always kind of inituivily have done that anyway. Or rather, when programming in any other language, like PHP or C, I just create files for every class with the same name, and I also prefer using lowercase/underscores everywhere.

  19. bootdisk says:

    IMHO, in java is not necessary to make the class named like the file unless they are public.

    And you can declare more than a class in a file but just one of them must be public.

    Maybe I’m wrong because I use java within a preprocessor.

    Did you try D? It’s awesome… its like C++ should be.

  20. gap says:

    Hi John.
    You should post more insights about programming.
    I’m a big fan of your work in Doom/Quake, I learned a lot just studying the source code of those games. Currently, in how many languages do you code? What’s your favorite programming language?

  21. anonymous says:

    from the link i gave few post above:

    “Using the Flash 9 Public Alpha’s compiler, you can define a .as file with multiple classes defined in a single package block, and it compiles and runs just fine.

    You also don’t have the restriction that class names must match file names, or that files must be in a folder structure corresponding to their package structure.

    In fact, you can even have multiple package blocks in a single .as file, as long as the packages all appear before any classes defined outside of packages (I’m not sure if that’s a bug or intentional behavior).”

  22. balls says:

    Show me a language that doesn’t have it’s own little idiosyncrasies and I’ll buy you a lap dance.

    Even the best language in the world (python) requires you to declare “self” as it’s first parameter for member methods.

    Heck, standard C++ conventions have you declare one class in a headerfile, and name the headerfile the same as the class, so it’s not like this is coming from left field.

  23. forrest says:

    Yeah, but at least C++ will compile if you name the header file something different, as long as you get your includes right.

  24. balls says:

    Even if the compiler lets you, Scott Meyers will come into your room at night and kick you in the dick.

  25. rajesh says:

    Yep, its the same as Java. I think their docs specifically mention this. Even AS 2 had some rudimentary support for classes and it was done the same way. Anyway, this is a programming convention by some IEEE standards or something like that (I’m not too sure) that these languages are designed around.

  26. anonymous says:

    I’m never a fan of having code organization forced on you by the designers/authors of the languages you choose to use. It strikes me as being lazy on their part to not take them time to allow for others to do things how they want. But as you know, the best programmers seem to be the laziest programmers happy

    It’s always best to allow the user of your product to choose how to do that. Way back when on the old 8bit machines I used to write all my 6502 code in uppercase and all my Z80 in lowercase. Why? It was another mental trigger for me as to which language I was writing in.

    LDA #80

    versus

    ld a,80

  27. josh1billion says:

    Haha, this exact thing frustrated me when I started learning ActionScript. Because of this and other ways ActionScript strays from conventional game programming, I haven’t yet bothered to get too in depth with it.

  28. Dooley says:

    I’ll stick with AS2.0 for as long as I can, thanks.
    Life is simpler, and there’s more documentation.

  29. rsweenie says:

    I hate when programmers ruin scripting languages! It seems Nobody wants simple Flash developer anymore…They want Flex developers. I’m a web developer, and I haven’t found a need for Flex yet.

  30. TAH says:

    A good compiler error message can save a lot of time and frustration.
    And in this category the worst thing is C – nothing can beat that – not even AS3. winking

  31. Wes says:

    I think this is part and parcel of learning a new language. It is a bit of an odd requirement, and you’re right, not required and it appears to have been “borrowed” from Java for no apparent reason.

    Class definitions are compiled into the SWF at run time and can be loaded via “getDefinitionByName” if you know the full package definition of the class. Provided you have referenced the class at least once in code or forcibly included it via a resource file.

    Overall, I think AS3 is a reasonably good language. It’s very “flexible” and you can just pump out code with it once you’re past its idiosyncrasies.

    That said this is an old post, the tools have gotten significantly better since you wrote this too. happy

  32. Bryce Pelletier says:

    Man, this gripe is pretty weird. I learned that the file name had to be EXACTLY the same as the class name in the very first class tutorial that I looked at for as3. But I have also heard in a number of times since. That really sucks that you didn’t run across that as3 convention sooner. I have found that Adobe is bad about assuming that everyone knows this stuff, but as3 has served my purpose.

    I really wish I could just pick up C, C# or C++ and run with it, but alas as3 was a language that helped me start learning programming. I do hope to graduate to some more hard core programming languages soon, but for now I have been able to learn some of the basics on my own with as3. I think in a few more years I may be ready for pointers and references and memory allocation.

    Cheers

Leave a Reply