Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid having messy AS3 code

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

like image 669
Mike Belotti Avatar asked Jun 09 '11 13:06

Mike Belotti


2 Answers

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.

like image 187
BumbleB2na Avatar answered Oct 19 '22 22:10

BumbleB2na


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.

like image 33
subb Avatar answered Oct 19 '22 22:10

subb