views:

157

answers:

2

When you build a flash piece from scratch using flash, and it's mostly AS3 and you are using all external .as class files, how do you structure your software? what type of global variable structure do you use? does your top level class always look the same? what line of code do you use to program attaching the main sub display objects to the root MovieClip?

+1  A: 

Programming AS3 should be similar in most aspects to programing in C#, Java, or some other OO language.

The top level class stays very similar and is modeled on Adobe Practices. It looks something like this:

package 
{
    import flash.display.Sprite;

    public class FirstCircle extends Sprite
    {
        public function FirstCircle( )
        {

        }
    }
}

Attaching code to the root MovieClip is accomplished in the IDE by entering the name of your class in the main properties within Flash.

Do not use global variables, use properties like you would in C# or Java:

private var _prop:String;

public function get prop():String {
    return _prop;
}

Hope this helps.

Todd Moses
I understand all that. What do you use for object to object communication? How does Main->Car->Dashbaord->Knob call Main->Car->Window->Open(), Knob obviously shouldn't have a window instance.
Ryan
myObject.myMethod.call(myOtherObject, 1, 2, 3);
Todd Moses
@Ryan one way is to do something like: in the Knob function dispatch a custom Knob.Pressed event when pressed. The Car object should be listening for this. When this event is recieved the car object will call Window->Open()
Allan
+2  A: 

There are basically two ways to handle architectural questions in OOP Flash. The standard model that any serious developer should become comfortable with uses custom events. In your above example, your windowKnob then doesn't know about the window - it just knows if it has been pressed. When pressed, it dispatches a "knobPressed" event. The Car object catches that event and then tells WindowManager to open the associated window.

Events are a bit tricky to work with at first because it's not obvious how to send a data payload with them. Maybe your knob can either be pressed up or down, but only sends one event. How, then, does the car know whether you want to open or close the window? Because you extend the Event class to created WindowKnobEvent, and on WindowKnobEvent you expose a public property called "direction" which can be set to either up or down. When that event fires up and gets passed to your WindowManager, maybe you have a switch statement set up there to behave differently based on the "direction" property - does that make sense?

So 95% of the time you're going to want to use the Event model to propagate data throughout your application.

Sometimes, though, it can be appropriate to use callbacks. In other words, you would pass your WindowKnob object a reference to the OpenWindow function in your WindowManager. Since Functions are all objects, you would simply say var openWindowFunction:Function = WindowManager.openWindow, then pass openWindowFunction to your knob class.

THIS IS GENERALLY A BAD IDEA: it means that your code is tightly coupled. It means you can't take that knob and drop it into a different car - you'd break it, since it would no longer be able to hold a reference to the first car's WindowManager. However, in certain circumstances callbacks can be more efficient. In mobile device optimization, for instance, it's generally better to minimize the number of objects being created. Every Event you dispatch uses memory. It's a bit more efficient to establish your callback reference once and then let it run without creating a new Event object every time someone pushes the knob.

Does that help? I mean, AS3 OOP Architecture is hardly something that can be easily summed up in a single SO response - but I guess the gist is this: learn how events work and everything will fall into place. Don't use callbacks unless you understand the implications.

Cheers, and I hope that helps! myk

Myk
great answer. i'd like more details on the custom events structure. but i have a more general question. how do you structure your display object hierarchy. if i have a car, and the car has an engine, a door and a trunk then I would do this:<code>package videogame { import flash.display.*; public class Car extends MovieClip { private var engine:Engine; private var door:Door; private var trunk:Trunk; public function Car() { addChild(engine = new Engine()); ... } }}</code>is it necessary to keep instance variables for the children?
Ryan
crap that didn't work. can you do code in comments?
Ryan
I get the gist of what you're saying, no worries about formatting. The answer isn't really cut and dry - I mean there are a million ways to do things - but generally speaking when you have a hierarchical object like that then instance variables are a solid approach. Your car can have a member "Dashboard" which in turn has members "Speedometer" and "Radio", etc. There are other ways to approach the problem - you could have a ComponentManager class, or a CarPartsPool that only instantiates objects when you need them - but that's a higher level architectural discussion.
Myk
Depending on your degree of competence and comfort level with advanced programming concepts, you might want to consider an MVC approach using something like PureMVC. MVC Frameworks are designed to allow you to add something like for instance a window knob without having to go back and reprogram your car to listen for it. Basically, all logic is separated out from the visual parts of your car. Your display elements become little more than bitmaps, and an abstract Controller listens for their events and then directs them. Learn more here: http://puremvc.org/
Myk