flash oop_a_new_way
A new way of thinking
Object oriented programming should be considered as a way of thinking and of designing an application. Some people even consider it to be a programming paradigm. This new way of thinking isn’t limited to Actionscript 3 and can be applied to a large number of languages. Before starting we will familiarise ourselves with the vocabulary then we will look further into object oriented programming by concrete examples.
It was in 1967 that the first object oriented programming was launched called Simula-67. As its name would suggest, Simula allowed you to simulate all sorts of situations for applications of the time for management or accountancy applications. We owe the idea of objects, classes, sub-classes, events and the garbage collection to Simula. Alan Key, the inventor of the term object oriented programming and the engineer at Xerox, extended the concepts brought by Simula and developed the Smalltalk language now considered as the real starting point of object oriented programming.
The main interests in object oriented programming reside in the separation of tasks, the organisation and the reuse of code. We can summarise this chapter in one essential phrase: For each object type there is a specific task.
In order to develop an ActionScript program we have two approaches to choose from. The first called procedural or functional programming doesn’t define a clear separation of tasks. The different functionalities of the application are not allocated to a specific object, the functions are triggered in a precise order and assure that the application executes correctly.
To summarise, this is the first programming approach that any senior developer knew. Any bug or update was worrying as there was no way of isolating behaviors and bugs – welcome to spaghetti code!
The second approach called object oriented programming defines a clear separation of tasks. Each object looks after a specific task, in the case of a photo gallery an object can manage the server connection in order to get the data, another looks after the display and a final object takes care of the mouse and keyboard user actions. By connecting these objects, we give life to an application. We will see later on that the difficulties in object oriented programming reside in the inter-communication of objects.
The following illustration shows a photo gallery composed to three principal objects:
Even if this seems normal, the idea of object oriented programming remains abstract without a concrete case based on every day development. In order to simplify this we can look at objects that surround us. We can see the idea of task separation all around us.
In order to optimize the development and maintenance time, a program can be considered as a group of communicating objects which can be replaced or reused at any time. A simple car is the perfect example.
The engine, the wheels, the clutch and the pedals allow our car to function. If it breaks down each part will be tested and when one of them is found to be defective it is replaced or repaired allowing the car to work again. This is the same situation in programming, if our object application is buggy, the separation of tasks allows us to easily isolate the errors and to correct the application or even to add new functionalities by adding new objects.
Everything is an object
In the surrounding world everything can be represented as an object – a light bulb, a tree, or even the earth itself can be considered as an object.
In programming, we should consider an object as an entity with a state allowing it to perform operations. To simplify we can say that an object is capable of carrying out certain tasks and possessing specific characteristics.
A television can be considered as an object having a size, a colour as well as functionalities such as being on, off or on standby. Here too the separation of tasks is used, the cathode ray tube creates the image, the screen displays it, the captor generates the information sent by the remote control. This group of objects produces a functional and usable result. We will see how in this way we can design an ActionScript application by associating each object to a specific task.
In order to bring our television example into ActionScript 3, we define a Television class allowing us to create television objects. Each television object would therefore be a Television type. When a developer speaks of the Array class we know which type of object he is talking about and its capacities such as adding, deleting or categorizing elements. In this same way, by talking about the Television class we know which functionalities are available for this type.
If we look at the interior of a television, we find all sorts of components; speakers, buttons as well as a group of electronic components. In programming we can exactly reproduce this idea; we see that many objects working together can give birth to a concrete application. Each component can therefore be reused in another program; the speakers of one television model could be used in another.
This is what we call the idea of composition which we look at in the article entitled The broadcast of personalized events.
Once our object is created, we can ask it to carry out certain actions, when we buy a television we choose it according to its specifications and we expect to be able to turn it on and off, change the channel and the volume. These properties are defined by its interface which is directly linked to the type:
In order to instantiate an object from a class we use the keyword new:
// creation of an instance of the Television class in the myTV variable var myTV:Television = new Television();
The Television type determines the interface of our television, or in other words what the object is capable to do. The turnOn, turnOff, increaseVolume and decreaseVolume functions are called methods because they are attached to an object by defining its capacities. When they are called we execute a group of instructions which are transparent to the user. In the following example we instantiate a television. If it is too late we turn it off:
// creation of an instance of the Television class in the myTV variable var myTV:Television = new Television(); // we turn on the telly myTV.turnOn() // if it is too late if ( timeNow > 12 ) { // we turn off the telly myTV.turnOff(); }
How will we store the information such as the size, the colour or even the series number of the television?
This data represents the characteristics of an object and are stored in the properties. We can imagine that a television has a size, a weight, a colour and a series number.
All instances of the televisions created from the same class have the same functionalities but also have different characteristics such as the colour, size or even the weight. To summarise we can say that the methods or properties determine the type.
To get the colour or weight of a television we target the desired property:
// we get the size var size:Number = myTV.size; // we get the colour var colour:Number = myTV.colour;
In Flash we find the concept of objects all over the place. Let’s take the case of the MovieClip class, in the following code we create the MovieClip instance:
// instancing of a clip var myClip:MovieClip = new MovieClip();
We know the functionalities available on a MovieClip instance after the MovieClip type:
// methods of the MovieClip class myClip.gotoAndPlay(2); myClip.startDrag();
By creating multiple instances of the MovieClip we get the objects with the same capacities but different characteristics such as size, position or transparency:
// instancing a first clip var myFirstClip:MovieClip = new MovieClip(); // use of drawing myFirstClip.graphics.lineStyle ( 1 ); myFirstClip.graphics.beginFill ( 0x880099, 1); myFirstClip.graphics.drawCircle ( 30, 30, 30 ); // characteristics of the first clip myFirstClip.scaleX = .8; myFirstClip.scaleY = .8; myFirstClip.alpha = .5; myFirstClip.x = 150; myFirstClip.y = 150; // display of first clip addChild ( myFirstClip ); // instancing of second clip var mySecondClip:MovieClip = new MovieClip(); // use of drwaing mySecondClip.graphics.lineStyle ( 1 ); mySecondClip.graphics.beginFill ( 0x997711, 1); mySecondClip.graphics.drawCircle ( 60, 60, 60 ); // characteristics of second clip mySecondClip.scaleX = .8; mySecondClip.scaleY = .8; mySecondClip.alpha = 1; mySecondClip.x = 300; mySecondClip.y = 190; // display of second clip addChild ( mySecondClip );
If we test the previous code we get two circular clips with communal functionalities but with different characteristics as illustrated here:
In Flash everything is an object. All the classes allow you to instantiate objects with specific functionalities such as display management, loading of external data etc.
We will now discover how to design our own objects in ActionScript 3 in order to make them reusable and to ease their usage. In a new ActionScript file we will create a Player class which will allow us to represent a player in the game. In a few minutes our class will be defined and ready to use.
Worth remembering
- Everything is an object
- Each object has a specific task
- The methods define the capacities of an object
- The properties define its characteristics
- A class instance is an object created from a class
- To instantiate an object from a class we use the keyword *new**
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42






