Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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?</p> <p>So 95% of the time you're going to want to use the Event model to propagate data throughout your application.</p> <p>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.</p> <p>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.</p> <p>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. </p> <p>Cheers, and I hope that helps! myk</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload