Beginners ActionScript OOP Tutorial Part 2

November 7, 2008

For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Flash CS3 Training CD.

.

Beginners OOP with ActionScript – part 2

Using Objects

To use an Object Oriented approach, you need to learn to characterise your applications in terms of ‘Objects’, interacting with one another to deliver your program. With larger applications, this will be a ‘high-level’ analysis of the various program requirements, and is carried out before any programming takes place. For example, with languages for which Object Oriented development is often used, such as Java and C++, applications are normally built by a team of people. In these circumstances, each member/ section of a team will be responsible for developing specific parts of the application. When it comes to building the application as a whole out of these component parts, it is vital that the parts have what’s called a ‘well-defined interface’. This means that the different components can work together, without having to know each others details.

For example, when you use the Classes built-in to ActionScript as in the above example, you can create Objects of the TextFormat class and use its various methods easily, without having to understand how these methods are actually implemented in the underlying code. Similarly, when you create your own Classes, you will decide on the data and functions that any Objects of the Class will have, but the details of how these functions are carried out will remain relevant only within the Class itself. So in order for this approach to be successful, you need to ensure that your Classes have meaningful, clearly defined behaviour.

To demonstrate, let’s create a simple Flash application with a Class declaration. In this example, you are creating a movie that carries out various operations on text/ textfields (among other things). Given that this is a regular feature of your program, you decide to create an Object that will be responsible for all tasks relating to text; meaning that it will contain all text/textfield-related code. Your Class declaration might be something like this (further explanation follows):

class TextHandler
{
//class instance variable
var tf:TextFormat;

//constructor function - initialises any instance variables
public function TextHandler()
{
tf=new TextFormat();
tf.color=0xFF0000;
}

//function to format text with the TextFormat
public function formatText(txtField:TextField):Void
{
txtField.setTextFormat(this.tf);
}

//the class would also have other functions relating
//to text processing
}

This code should be saved in a file named ‘TextHandler.as’ identifying it as an ActionScript class file. Once you’ve defined a Class declaration like this, you can then create ‘instances’ of the Class in your main code; these are the ‘Objects’. You can then use the Objects to carry out the functionality contained in the Class declaration. To do this, create a new Flash document and save it in the same directory as your TextHandler.as file, then create a dynamic textfield on the stage, giving it the instance name my_txt, and put some arbitrary text in it:

//in your main code - Flash file in the same directory
//as the class import TextHandler;
//create an object of the class TextHandler
var myHandler:TextHandler = new TextHandler();
//call a function on it - the movie contains my_txt textfield
myHandler.formatText(my_txt);

When you test your movie, the text should appear red.

To explain a little further about the Class declaration see part 3 of this ActionScript OOP Tutorial:


Entry Filed under: ActionScript. Tags: , , , .

1 Comment Add your own

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Follow us on Twitter

Categories