9
votes

I've been programming with ActionScript 3 for a little while and I've noticed that the natural progression of my code seems to take the form of one giant document class with dozens of member variables, callbacks, and handles to objects on the stage. In short: it's kind of a mess!

Thing is, I don't really see a way around it (not yet at least). I use different keyframes on the timeline to represent different states in an app, and while I have some code right on the timeline (for quick things like a mouse click on a movie clip), most of the logic just ends up dumped in the main document class.

So, I'm wondering... What are some good ways to help neaten up this code-gone-wild? Or is this normal? I come from a C++ background, and I like writing object-oriented stuff, but I can't see a way to carry over that kind of structure to Flash. Any insight would be really appreciated.

Thanks

6

6 Answers

9
votes

You can apply many of your C++ skills to your AS3 project.

There are lots of tricks. Glad you brought up putting code on the main timeline. Instead of putting code on the timeline (this is way too common in AS2 programs and with AS3 you can completely avoid it) I would recommend thinking of each object as a separate class. Your movieclip that you are applying mouseclick code to, for example, could be an object created with its own class. Say the MovieClip is a graphic of a ball. You should be creating a 'Ball' class that extends (inherits) MovieClip class, and handle the mouseclick event within it:

package 
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class myObjects.Ball extends MovieClip 
    {
        public function Ball ()
        {
            this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        private function mouseDownHandler(event:MouseEvent):void
        {
            // Code
        }
    }
}

Then, find your MovieClip of the ball in the Library pane, right click it, Properties, switch to Advanced mode, check off Export for AS. Now, notice how your MovieClip already references the MovieClip class as its Base Class? You won't need this any more since your Ball class extends the MovieClip class.. so in Class field write, 'myObjects.Ball' and clear the Base class field. You should see a green checkmark if you wrote a path to your namespaced Ball class that the Flash IDE can locate.

Now your Ball class will use that MovieClip so when you create a new instance of Ball in your main class, you can work with it like a MovieClip and attach it to the Stage, dynamically. Or, you could just add it in the timeline by manually dragging the Ball MovieClip on there.

Extending a class I explained is AS3's version of 'Inheritence' (Ball class inherits the MovieClip class). You can also use other OO-concepts like, polymorphism and encapsulation. You should encapsulate your code in to separate classes wherever possible. Say if you had a few different types of ball MovieClips in your project, and you want Ball class to be a parent class to Soccer ball, Pool ball, and Baseball. Each of those child classes could extend Ball class. Another thing I have found useful for large projects is to create a static class to handle all of my application's events. Since I define it as a public static class I can import it in to each of my classes and its variables are only created once for the duration of the application. This can be extremely useful.

I have also created my own pseudo-destructor in classes in an effort to work with AS3 more like c++. The easiest way to pull this off is to call the pseudo-destructor before you destroy the instance of an object. I did make it happen automatically in one application, so if this interests anyone I can track down the code.. but AS3 handles garbage collection behind the scenes and usually a destructor is not needed, but maybe I just think it isn't needed because I developed bad habits out of programming in AS3 for too long.

Personally, I think the more you strive to develop apps in AS3 as if you are developing in C++, the more fun it gets and the more reusable your code becomes. Keep it up.. soon instead of a mess of code you will have a mess of actionscript files lol.. bit of a double-edged sword but whatevs.

2
votes

Well, the first thing you can do is wrap each application states in a MovieClip. You will then be able to assign a class to this MovieClip, and do you're wiring (events & stuff) there.

Personnally, I would ditch the timeline all together. Here's how I work :

I create a pure ActionScript project in Flash Builder. I add any external libraries in a lib folder. I create a fla in Flash Pro for my assets. This fla exports a swc, which is linked to my Flash Builder project.

You can also try out RobotLegs. It's a tiny MVC framework that helps you wire your application.

2
votes

First if is not realy a must or you can't do with code, don't use timeline and elements on stage. My fla have only 1 layer with 1 frame and nothing on stage. But i do have all graphical elements that i need in library linked to classes. If i don't do much with those elements, i leave their base clas as is (movieClip most of time), but if i plan to do some manipulation with them i link them to custom class.

I also organize my lib folder, according to their needs. Example:

lib
  -> gui
    ->Controls.as (Controls class)
    -> etc..
  -> elements
    -> Ball.as (Ball class for controling bal element)
    -> Hammer.as (Class for controling hammer)
    -> etc..
  -> animation
    ->Tween.as (custom tween class)
    ->etc..
  -> data
    -> XmlParser.as (class for getting data from xml)
    -> etc..

Ofcourse this structure is just an example and can be organized in any way you need.

Beside that i try to separate logic part from display part and sometimes also separated data part.

With every project i build my own external library in which i put reusable classes created for current project and i know or i think they will/can be used in future projects.

1
votes

Use an MVC framework. Some find them limiting, but everytime I don't use one for a "simple" project, the scope grows and I scream in horror at the bad decision I made.

A good framework frees you from making decisions about certain application aspects, and allows you to implement things in a consistent manner.

A good framework also allows you to reuse things in other projects.

A good framework already has tons on man-hours sunk into it and will be better thought out than most people can put together without the the same effort.

I use PureMVC, but any of the popular ones should work.

1
votes

Since your from an OOP background you shouldn't be having this problem. However that is easily fixed with a quick read through a couple great books.

Essential ActionScript 3.0 http://www.amazon.com/Essential-ActionScript-3-0-Colin-Moock/dp/0596526946

ActionScript 3.0 Design Patterns. http://www.amazon.com/ActionScript-3-0-Design-Patterns-Programming/dp/0596528469

With your OOP background these are perfect.

If your getting one big file the obvious think to do is to begin by breaking things down into classes.

You would be wasting your time with MVC or any other design pattern without being able to reliably code OOP in AS3.

1
votes

Check out Robot Legs - it's a great MVC framework that really helped me write cleaner (and less!) code than I would normally.

I would also suggest Robert Penner's Signals library for three reasons: 1) It lets you write less code 2) classes no longer need to extend EventDispatcher 3) I freakin' love it!

I can't stress using an MVC framework enough - the file/folder structure it provides to organize your code is worth it alone.