Beginners OOP ActionScript Tutorial – part 3

November 7, 2008 at 12:15 pm 1 comment

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 Beginners Flash CS3 Video Tutorial.

.

Instance Variables

Instance Variables are variables that are contained in each ‘instance’ of the Class; so every instance of the TextHandler Class will have it’s own TextFormat variable. Instance Variables should be initialised (given an initial value) in the constructor function, which is called whenever an Object of the Class is created. It is important, however, to refer to the instance variables outside the constructor, as in the above example, so that they are ‘visible’ to the entire Class.

Constructor Function

When you use the keyword ‘new’ to create an Object of a Class, what actually happens is that the ActionScript code within the constructor function for the Class is executed, returning an Object of the Class. The constructor function does not have to have the return type specified, as it is identified by having the same name as the Class itself.

To illustrate, imagine you wanted to create several of the TextHandler Objects, each with different formatting (i.e. different colour, font etc); you could alter the Class declaration so that the constructor function takes parameters indicating e.g. the colour, and these properties of the TextFormat object could be set within the constructor code. This way your main application code could create several different Objects of the TextHandler Class, each handling the textfields that you want to format in the same way (make the changes to your Class declaration and Flash document code):

//class code constructor
public function TextHandler(col:Number)
{
tf=new TextFormat();
tf.color=col;
}

//main code creating object
var myHandler:TextHandler = new TextHandler(0xFF0000);

To test this, place another dynamic textfield on your stage and name it other_txt, again with arbitrary text in it. Now add the following to your Flash document code:

var otherHandler:TextHandler = new TextHandler(0x0000FF);
otherHandler.formatText(other_txt);
//When you test the movie, other_txt should appear blue.

This trivial example may appear odd at first, but once you have gotten into the habit of thinking in terms of Objects, you’ll soon see how the techniques can save you a lot of stress. Experiment within your applications by creating Classes and Objects, with the following additional guidance:

  • Functions can have as many parameters (of any datatypes) as you need, but can only have one return type; you must declare this when you devise the method (as in the stripChars function above where the return type is specified as String).
  • The functions within a Class that are declared with the keyword ‘public’ are those that can be called on any Object of that type, wherever it has been created, as with the formatText method above.
  • Additionally, Classes may feature private functions that are for use within the Class only; these are referred to as ‘helper’ functions and are used in much the same way as the stripChars function was in our original example. You will use such functions within your Classes where processing is repetitive or where the code contained within a function becomes overly long or complicated.

Class Overview

Look again at the (amended) version of your TextHandler Class declaration. In terms of the interface, the Class contains a constructor function (which takes one parameter of the type Number, representing the colour) and a single function for formatting text, which takes one parameter of the type textfield, and returns nothing.

Try adding more functions to the Class – e.g. add the stripChars function, using the keyword ‘public’ in front of it, then try calling it from your movie, passing it a String as in the original example.

In theory, the TextHandler Class could have been created by someone else and you could still use it as long as you know: what the Class is for, what the functions are called (and what their purpose is), what parameters they take, and what data they return, if any. If you think about it, this is all you know about the built-in Classes that you use regularly.

The ‘interface’ can also be made more explanatory through the use of documentation and comments, as is the case with the built-in Classes. You can see an example of this with the official Adobe documentation for the TextFormat Class in ActionScript 3.0 here: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextFormat.html

- this gives you limited insight into the TextFormat Class declaration.

As with the stripChars helper function example above, if at any stage during your development process, you decide to change any of the implementation details within your Classes’ functions, you should be able to do so without upsetting any other code, as long as your interface remains the same. e.g. If you decided to change the for loop in the stripChars function to a while loop (for whatever reason) it wouldn’t affect the rest of the code, as long as it performs the same function.

Summary

The Object Oriented design method is a good way to achieve well-organised applications, but the success of the technique does depend strongly on Classes being defined in a clear manner. If you’re struggling at all to grasp the technical notions, have a try at using Classes anyway; as long as you stick to the principles outlined above, you should be able to define and create useful Objects.

It can be challenging to understand these notions when you’re starting out as a programmer and have built only relatively small applications. However, even where you are developing applications single-handed, Object Oriented design can make for efficient programs that are easily manageable.

If you’re interested in exploring Object Oriented development further, advanced areas to study include: Packages; Inheritance; Polymorphism; Interfaces; Abstract Classes.

Advertisement

Entry filed under: ActionScript. Tags: , , , .

Beginners ActionScript OOP Tutorial Part 2 Flash CS3 Tutorial – Library Symbols

1 Comment Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Follow us on Twitter


Follow

Get every new post delivered to your Inbox.