开发者

Flash CS AS3 OOP Software Architecture

开发者 https://www.devze.com 2022-12-21 04:37 出处:网络
When you build a flash piece from scratch using flash, and it\'s mos开发者_高级运维tly AS3 and you are using all external .as class files, how do you structure your software? what type of global varia

When you build a flash piece from scratch using flash, and it's mos开发者_高级运维tly 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号