OOP - FAQs, Resources & Best Practices
So a while back, this thread was started:
http://board.flashkit.com/board/show...93#post3801593
In response to it, this thread (as in the one you are reading) was started.
Purpose:
The purpose of this thread is to provide resources, tips and best practices for those interested in learning more about Object Oriented Programming (henceforth OOP).
Mostly we want to focus on OOP in relationship to ActionScript, but references to general OOP best practices are welcome as well.
Expectations:
People are going to differ as to what they regard as best practices. So what you are going to get are a collection of opinions on best practices. The thread is by no means meant to be gospel.
If you are referencing some external resource or book please provide a reference or link.
Hopefully we will have enough experience collectively to make this a useful thread.
So let's kick it off:
Resources: (Here is where I go/ I will add more to this later.)
General OOP:
Wikiversity School of computer Science:
http://en.wikiversity.org/wiki/Topic...ed_Programming
OOP in a Nutshell (for which I may be accused of being a nut):
So this is meant to be a very basic introduction, so I am going to be abstract (something those of you familiar with OOP should know the benefits of, so go easy on me).
Basically put, Objects are related pieces of code in a specified syntax that perform a set of related tasks. Objects are created by creating class files (in AS these are external ActionScript files). If you have done even simple coding in Flash, you have probably done something like:
var myVariable = new MovieClip();
In this case we have created a variable myVariable that is an instance of the MovieClip object. We know this because of the new key word. When you use the new keyword you are creating and instance of an object. So what exactly go created? Well take a look at (assuming you are on a Windows Machine with Flash 8) C:\Program Files\Macromedia\Flash 8\en\First Run\Classes\FP8\MovieClip.as. , BUT DON’T EDIT IT. Notice it is more or less a collection of variables and functions (in this case mostly abstract methods, but more on that in a later post)
So why do this, well I can create multiple instances of a movie clip from the same AS file without having to reproduce the code in that file. All instances of this object will have the same variables (aka properties) and functions (aka methods). Once I have multiple instances of a class (aka object) . I can then apply different properties (e.g., set different alpha levels) and call specific behaviors (via methods) for each instance. I can do this because of the design of the MovieClip class. Basically, the MovieClip class defines all of the properties and behaviors expected of a MovieClip. Once I have created and instance of one, I can then set those properties and call the behaviors for each instance of that object.
So what does a class look like?
Well you can take a look the classes in C:\Program Files\Macromedia\Flash 8\en\First Run\Classes (Again DON’T EDIT them). These are the basic classes that come with Flash and these are actually what you are calling when you use that new keyword (there’s more to it, but we will get to that in a later post).
The basic syntax of a class is:
AS 2.0: File Name would Be ClassName.as
class ClassName {
function ClassName(){
}
}
AS 3.0 File name would be ClassName.as (and located in foldername under the root class path, more on this in some later post as well)
package foldername {
class ClassName{
function Classname(){
}
}
}
Few things to note and then I will sign off for this post:
The name of the AS file, the class name and the constructor function name (the one I bothered to write out in the examples - this can be one of many functions) must all match. These will also match how you use the keyword new. So for these examples you would create an instance by:
var myVar = new ClassName();
which actually ends up calling the constructor method. By having all those names match, Flash knows what file to refer to and what method to call.
OK I know that was really abstract (and long) and I left a bunch of things out (like data typing and what a class that actually does something looks like), but hopefully this will be the first post of many.
Bye for now and feel free to contribute.
1 Attachment(s)
EventHandlers - EventListeners
Event Listeners:
Ok so most of you are probably familiar with doing some thing like:
Code:
on (release){
//do something
}
OR
Code:
myButton_btn.onRelease = function () {
//do something
}
OR
Code:
function doSomething(){
// go ahead guess what goes here
}
myButton_btn.onRelease = doSomething;
All of the above are simple examples of using an event (in this case the on release event) to trigger some code. You can even get a bit more complex by doing the following:
Code:
var iHearYou:Object = new Object();
iHearYou.change = function (evtObj:Object){
//do something
}
myTextInput.addEventListener(“change”, iHearYou)
In the above example we actually created our own object and assigned a function named change. Then we added and event listener to a text input component that registers our object to listen for the change event. Hence every time the text is changed in our text input we have added the listener to, the change function in our object will be executed. This example illustrates several important things.
1. A listener is actually an object with specific functions that get executed when an event is broadcast. Generally the function name matches the event to be listed to.
2. The object that the listener is added to must dispatch the specified event. If it does not, the listener will never be triggered.
So what are the benefits of using event listeners as objects?
Quote:
Event listeners provide a more flexible event handling model than change handlers in Flash MX. For example, a listener object can receive events from several components; likewise, a component can broadcast a single event to multiple listeners. For more information about handling component events, see About Component Events in the Using Components Guide.
(From:
http://www.adobe.com/devnet/flash/ar...04_print.html)
So how does this work with classes? Well let's look as some examples. For this we will use two custom made classes (you can download these to play with them, just remember to set up you class path – see previous post for link to instructions for doing this):
Code:
class Ear
{
//I am a bit nosy and like to listen in
function onKeyDown ()
{
trace ("I hear you pressed key number " + Key.getAscii () + " pressed");
}
function change (evtObj : Object)
{
trace ("Lisenting to " + evtObj.target._name + " type." )
}
function focusOut(evtObj:Object){
trace ( "I hear that " + evtObj.target._name + " says " + evtObj.target.text )
}
}
class Mouth
{
//I like to gossip.
function onKeyDown()
{
trace ("Let's talk about key number " + Key.getAscii() );
}
function change (evtObj : Object)
{
trace ( evtObj.target._name + " is typing." )
}
function focusOut(evtObj:Object){
trace ( evtObj.target._name + " says " + evtObj.target.text )
}
}
Take notice of the following with the above two classes:
1. Both classes have exactly the same functions in terms of names, but trace different things. (i.e. in different listeners we can have the same function name, but do different things with them)
2. The function names must match the events they listen for. This will vary based on the object that you will register the listener to. In this case we will be using the Key object and TextInput components as examples.
3. Listener functions can receive the object that triggered them as a parameter, hence I can access the properties of the triggering object (see change and focusOut functions in example classes). Hence if I wanted to, I could vary my code based on the object that triggered it.
So now we have out classes: In Flash we can now:
Code:
var nosy:Ear = new Ear();
var chatty:Mouth = new Mouth();
Key.addListener(nosy);
Key.addListener(chatty);
bob.addEventListener("change", nosy);
bob.addEventListener("focusOut", nosy);
bob.addEventListener("change", chatty);
bob.addEventListener("focusOut", chatty);
jack.addEventListener("change", nosy);
jack.addEventListener("focusOut", nosy);
jack.addEventListener("change", chatty);
jack.addEventListener("focusOut", chatty);
jill.addEventListener("change", nosy);
jill.addEventListener("focusOut", nosy);
jill.addEventListener("change", chatty);
jill.addEventListener("focusOut", chatty);
While the above could look a bit messy, what it is meant to illustrate is the following:
1. You can register multiple objects to the same listener.
2. You can register multiple listeners to the same object.
So basically I can trigger multiple functions in different objects all with the same event, or multiple objects can trigger the same function. I don’t have to rewrite it for each object.
Since this is getting a bit long, if you want to see how this works, download the attached files and play with it by commenting out some of the lines in the Flash code above to see how what gets traced out changes.
See also:
http://livedocs.macromedia.com/flash...=00001375.html