Transition from Procedural Programming to Object Oriented Programming

There are few small topics that you have to learn before you start programming OOP. Before I start I want to mention a feature which same with Flash CS3. With Flash CS3 we were not able to write codes on Buttons or MovieClips. This was not a good feature for lots of developers. But I find this feature very useful, because as a developer I am not favor of seeing 100 pieces of code all around of one simple project. Of course there is no such a rule which say; you have to write all code in 1 frame”, but writing code on each button and movie clip which is added to project is also not a good work style. Anyway in this article I will cover how to use Document Class, Library Objects and Classpath.

Document Class

With Flash CS3 new feature introduced called “Document Class”. With this feature now we can integrate class files without adding any code in timeline. Of course if you want you can still use timeline but as a developer you have to decide it is necessary or not.
Usage of Document Class is very simple. First you have to make a class file that you want to use as a Document Class. Than you have to add name of this class in the Document Class property of the flash document which you can reach from Properties panel.

Here is a simple class for purpose of testing;


DocumentTest.as

package {
import flash.display.Sprite;
public class DocumentTest extends Sprite {
public function DocumentTest() {
trace("Document Class is working…")
}
}
}

You have to open new Actionscript file and than the code above. Than you have to name DocumentTest and saved. Afterwards create new flash document and save in same folder. And now what you have to do is add the name of class in the Document Class field in Properties panel.

Document Class

Now you can test your flash document. When you test you will receive a message from output panel which is in the Document Class. So this is all, not even a single line of code in flash document.

There are couple details that you should take care when you are using document class. First of all the document class must extend Movie Clip or Sprite class. In the example above I extend the Sprite class and also import Sprite class. If you have more than one class structure you don’t have to extend all classes as Movie Clip or Sprite class. You should do that for document class unless is not necessary for the other classes.

When you type name of your class name in document class properties and test your flash file document class runs automatically. How ever if you want to do this manual or let say you want to control this option than by adding the extension name (.as) n the document class properties you can use it manual. Than all you have to do is create instance of the class.

var d:DocumentTest = new DocumentTest();

Library Objects

One of the new features came with Actionscript 3.0 that we can use the library objects as a class instances. All you have to do is give linkage ID to object in library. If your object is image it will get Bitmap class as a base class, if your object is Movie Clip it will get MovieClip class as a base class. For example; draw a circle on stage and make a movie clip. Than open your library and right warning message lick on object, from menu select Linkage;

Library

Select the “Export For Actionscript” on the window which appear. Than you can enter the instance name that you wish top art which says Class.

Linkage

When you click ok warning message will appear. Reason of warning message is because there is no class exists by the definition\name you give that’s why it will use the MovieClip as a base class. You can click Ok and finish library part. Now on all you have to do is create instance of library objects.

var lib:library_test = new library_test();
addChild(lib);

Classpath

There are two ways of using classes, first copying classes that you want to use in same folder of flash document. This is not a big deal but when ever you start new project and you need those classes you have to copy and paste those files. Second option is using class path. Class path is a folder path which contain Actionscript classes that you use or create.

By using class path you don’t have to copy and paste every time you use some Actionscript classes. For adding class path select;

Edit > Prefences or press Ctrl + U from keyboard. In following window click Actionscript from left menu. Than click “Actionscript 3.0 Settings” from “Language” section (Left, Bottom) , soon as you click window above will appear;

Classpath

Now one thing you should not do is editing or removing $(AppConfig) section. In case if you delete you wont be able to use Actionscript main classes Over here all you have to do is click button with the plus sign and than new path field will open. Than click new added area and select folder which contain your action script classes. Now on you don’t have to copy and paste your class at all.
Ps: The visual materials used in this article have been taken from Actionscript 3.0 book. Because of copyright is not allowed to use materials in this article without permission.

Up Shot

In next article of this series I will explain basic information about class structures such as methods, functions, properties and etc.

Take Care
Engin!

One Response to “Transition from Procedural Programming to Object Oriented Programming”

  1. WaxWheell 25 November 2009 at 19:49 #

    Cool post, I didn’t thought reading this would be so interesting when I saw your title.


Leave a Reply