AddThis Social Bookmark Button
 Should you learn Object Oriented Programming with Actionscript ? (part 1)

You have heard about Object Oriented Programming before (OOP) but you are not too sure what it is and if you really need to learn a bit more about it. You have heard that if you want to use Actionscript 3.0 you will need to learn OOP and you are now wondering if you should really start reading about it. Well good timing because it is for you that I am writing this article.

Flash is OOP based


If you have programmed in actionscript before then you will already have done some OOP probably without knowing it. When you create a Movie clip on the stage, give it an instance name and add an onPress event to it, this is OOP :) This Movie clip is an instance of the MovieClip class and you are using the onPress method of that class on that particular instance, this is Object Oriented Programming.

In Flash, movie clips, text fields, sounds, graphics are all objects that are created from a pattern called a class. In actionscript 2.0 you did not need to worry about these classes because they are native to Flash, they don't need to be imported or created and work in the background. When you drop a movie clip on the stage it automatically creates a new instance of the movie clip class for you.

OOP is all about classes, objects, properties and methods, but before that it is a conceptual approach of programming that is supposed to help us to develop, maintain and organize our code. When you will know more about OOP you will just realize that it is a very natural way of programming and I am going to explain why.

OOP: a natural concept

As I stated previously OOP is all about classes, objects, properties and methods and the relations between these different aspects.

A class can be characterized by a collection of methods and properties that is shared by different objects, as simple as that.

Solar systemIf you look around you, and even how the universe is organized, you will notice that OOP is not really coming out of nowhere and that everything that surrounds us can be described using the OOP terminology.

You could for example define a class called PlanetClass which would correspond to a generic set of patterns (properties) and behaviors(methods) that would apply to all our planets in the solar system.


You could for example define the following set of properties in the PlanetClass : size(number), revolution duration(number), distance from the sun(number), does it have an atmosphere (true or false), does it have satellites (true or false)..etc

Each planet has properties but also has behaviors or actions like: rotation, revolution that we could define as methods of the PlanetClass.

So basically all planets are instances of the PlanetClass. The solar system could also be an instance of the SolarSystem class with its own properties that would contain all the instances of the PlanetClass

Another example would be the human body which acts as a whole but with different parts that have their own properties and methods. so we could have a Body class with its properties and methods but also a finger class, a stomach class, each with their properties and methods.

The diagram below illustrates the concept of OOP.



So now I am sure that this all sounds familiar. It's like having movie clips nested in other movie clips all with their own properties and methods, if you have been using Flash for a little while you have actually been working in an OOP environment without knowing it.

OOP vs procedural programming

Procedural programming is just the way Flash developers program if they are not using OOP. Procedural programming consists of writing a list of instructions telling a computer, step-by-step, what to do, usually having a linear order of execution from the first statement to the second and so forth with occasional loops and branches. Procedural programming languages include C, C++, Fortran, Pascal, and Basic and if you are willing to do so ... actionscript.

There is nothing wrong with procedural programming but things can quickly get out of control in Flash especially when we have many graphic objects and sometime we are not very sure where the code is sitting for that particular object, it could be on the timeline, on the instance or in the instance. Furthermore in procedural programming you could easily end up with more than 1000 lines of code where it can get tricky to keep a clear picture of what is doing what.

Some of you working in the Flash industry might have had to amend or update the code of another Flash developer and you know how tricky that can be if the code is more or less randomly dispatched in the FLA. In that case you can actually spend hours chasing chunks of code and trying to understand the connections between all of the bits.

- If you are using OOP the first interesting feature is that you will write your actionscript in external AS files and that means that all your code will be sitting in a dedicated directory, so no more headaches to try to find out where it sits in the FLA.

- The second good point is that you can organize your code in a better way. You can modulate your code to make it a lot more easy to understand for another developer. Each object on the stage can be connected to a template class that will define its behaviors and properties.

- Another good reason to use OOP is code reusability, if you have written a little class that animates a sprite you will be able to re-use that code without having to modify the class and that can save you a lot of time.


Actionscript 3.0: a good reason to go OOP

With AS3.0 actionscript is becoming even more OOP based. You can still write your code on the timeline but complex projects will really need to be handled with a clean and organized OOP approach. With AS3.0 a lot more control has been given to the developers but this also adds a little layer of complexity (at least when you start learning it) because you now need to import the classes that you are going to use in a project, this wasn't the case with AS2.0. But you should find your motivation in the fact that AS3.0 is a lot more powerful and a lot faster than AS2.0, now a lot of complex projects can be created and a lot of dreams can come true. In Part 2 of this article we will have a look at OOP from a more technical point of view.
AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved