A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Class structure woes

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    37

    Class structure woes

    Okay so I'm having some problem with class folder structure/hierarchy/something.
    I'm working on my RPG combat engine, and everything is coming along nicely, except I'm starting to fill up my game folder with .as class files, and I was thinking it's probably smart to rearrange it so I have a folder for characters/enemies, and one for levels etc etc. I'm kinda learning as I go here so thats why I didnt start out with a planned folder structure.
    Anyway, I can't seem to understand the correct way to go about doing it.

    Example: I have three classes which extend a "Character" class. They are added to the stage by the "Arena" class, when the Arena object is added to the stage by the "Game_Master" class. All of my classes at this point start with just "package {". How do I put the character files in a folder called, say, "characters"? I've tried a bit, but I can't get it to work. I moved them to the folder, made them start with "package characters {" and tried to "import characters.*;" in eventually just about every class, when just importing them to the Game_Master and Arena didn't work.
    What's the correct way to do it?

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    the package name must reflect the directory structure. what you describe near the end of your post should be right.

    assume you're FLA and Document Class are in the "root".

    if you have CharacterA.as in the characters/ directory, the package name should be "characters". you'd import it into other packages with import character.CharacterA; this same import statement is not relative - if characters/heros/Hero.as imports using the same path as a package in the root directory. only if two classes are in exactly the same package (and therefor directory) do they not require importation. if there's any level of difference, they must be imported using the fully qualified class name. that's to say that characters.heros.Hero (characters/heros/Hero.as) would use import characters.villains.Villian (characters/villains/Villian.as), even though both share the parent directory of characters.

    traditionally, packages are named using the domain of the author, backwards. so if your homepage is "flashkit.com", you'd use a base folder for your classes named "com", then another folder inside that named "flashkit", then folders inside that specific to your generic class role (like "events", "display", "transitions", etc). then you'd import using com.flashkit.events.MyEvent;

  3. #3
    Member
    Join Date
    Sep 2010
    Posts
    37
    So, what you're saying is that ALL my classes need to import EVERY other class in the library? Even if they don't use it? Like, I have a class that takes care of damage calculations. The Arena class, which contains the character classes, uses "getStat" functions in them to put variables through to this damageCalc class, but the damagecalc class never actually "contacts" the characters, just uses the variables. I can't get it to work just by importing it to the arena, so, do EVERY class need to import them? that seems like a lot of work, if so I'd rather have the classes just crammed together in the root folder anyways.

    EDIT: Nevermind! I figured out the error. I just didn't define the Base class over again in the linkage properties! That class was just defined as "Enemy" as they were already exported, but changed it to characters.enemies.Enemy, and now it works Also, does the package name have to be "package characters.enemies{"? or can it just be "enemies", so i can move the folder later without having to edit each class package name?
    Last edited by Jumpkut; 10-27-2010 at 08:24 AM. Reason: Got it!

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    glad you got it working.

    to the follow-up questions - if you move the class to another folder, you'd have to change the package name in the .as and in any other imports. the package name must reflect the directory structure. yes, this can be a big pain - you'll definitely want to figure out your class heirarchy and architecture before getting deep into your application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center