This is a quick-step guide for Actionscript developers moving from procedural programming (timeline programming in Flash) to Object Oriented Programming in Actionscript 3.
In the good ‘ol days Flash was the tool of the trade for designers and animators, with only a limited support for scripting.

The scripting facilities were basically a tool for controlling the different timelines in MovieClips, adding actions to buttons and opening links.

Since then, Flash has moved towards a full featured Object Oriented Development platform, introduced with Actionscript 2 and 3.

I’m writing this entry, because I have experienced numerous Flash developers, having a hard time taking the big leap into Object Oriented Programming.

The problem with timeline code

Procedural programming (or timeline code in Flash) focuses on handling concrete tasks, aiming directly at the end result. This style of programming is efficient for solving ‘now and here’ problems, getting the job done.

Timeline code in Flash can be placed inside every MovieClip symbol on the stage, referencing other nested symbols. In other words, procedural programming in Flash is the equivalent to a Chinese box or a Russian Babushka doll: Layer upon layer of hidden code.

In case the procedural programming project is growing, the developer will have to maintain a larger number of individual functions, increasing complexity dramatically.

This imposes numerous restrictions:

• Code is hard to locate for other developers, since it’s hidden on multiple timelines.
• Not suitable for large scale applications, since timeline scripts get too long, making the code harder to read.
• Functions are dependant on other functions, making code reusability difficult.
• Application is harder to debug: It’s easier to debug 50 lines of code in one class, opposed to 2500 lines of hard-to-find timeline code in one application.
• The application’s logic, data and presentation layer (view) are mixed together in one big pile of code, making it difficult to alter one part of the application, without affecting other parts.

The solution: Objects Oriented Programming

Objects Oriented Programming (OOP) introduces abstract relationships and hierarchies of related functionality, separating logic, data and presentation (view) into different classes, by creating custom classes, and thereby custom data types.

The Flash API (Application Programming Interface) providing basic functionality in Flash is a set of factory class files, created by Adobe. Without knowing it, you are already making use of Objects Oriented Programming when using basic Flash objects on the timeline, like String or Array data types, since these classes are data types belonging to the Flash API.

When programming OOP-style in Flash, the code is placed outside the timeline, in external plaintext files with the .as extension. These are called class files and are structured by a set of rules; Just like a HTML document needs to have a and a section, an Actionscript 3 class file needs a package, a class body, a constructor and usually one or more methods.

All class files are organised into separate files (classes) and folders (packages) on the hard drive, representing each class’ functionality and relationship in the global class hierarchy.

Here’s an example of a very simple class:

1
2
3
4
5
6
7
8
9
10
11
package com{

    import flash.display.MovieClip;

    public class HelloWorld extends MovieClip{
   
        function HelloWorld (){
            trace("Hello World");
        }
    }
}


Leave a Reply

You must be logged in to post a comment.



workline