Hello World Program / Document Class

August 9, 2011 § Leave a comment

So like all other languages the best place to start is hello world program. So without getting into various details just let us straight get our hands dirty in Actionscript.

    1. Open the flash authoring tool (Adobe Flash Professional CS3 or Higher)
    2. Create a new Actionscript 3.0 Fla File
    3. Save the file as HelloWorld.fla
    4. Go to File -> New ->Actionscript 3.0 classs
    5. Create a new class with name HelloWorld.as
    6. Copy paste the code given here
      package {
      import flash.display.MovieClip;
      public class HelloWorld  extends MovieClip {
      public function HelloWorld() {

      trace("HelloWorld");

      }
      }

      }
    7. Save The File.
    8. Now go back to HelloWorld.fla , under properties tab in a field called class, write HelloWorld as class name.
    9. Save the file and press ctrl + enter.
    10. You should see the HelloWorld Written in Output Screen.

Requirements for this program to run:-

  1. All the files should be in same folder.
  2. The name of class file should be same as class name (HelloWorld.as , HelloWorld).
  3. You should have adobe flash cs5 or higher.
  4. You should have basic knowledge about OOPs and should be familiar to terms like class, object etc.

Package:-

Package tells where your class resides, it’s the name of the folder where you have saved the file Helloworld.as, as for simplicity i saved the as file in the same folder as fla file you need not tell in which package it exists. But package is mandatory structure and you need to include it at the top.

Import:-

Import does not actually import the code from somewhere it just tells the compiler the place where the class resides. The document class that we have created actually replaces the main timeline as starting point of our executed swf file.  So to get something out of this we need to have a movie clip where we could perform our functions.  So we did import a sub class MovieClip from parent class Display inside the package flash.

Public:-

Public defines the access identifier of our class, it says that the class HelloWorld is public and anyone from any package could access it without any permission.

HelloWorld extends MovieClip:

This statement says that class hello world is an extension of the class movieclip which makes it behave and have the basic properties that movie clip would have. This includes basic things like X,Y co-ordinates and lot other stuff that we will check out in near future. HelloWorld is a class name and it should be same as that of file name (HelloWorld.as). Inside the braces everything is class definition.

Public function HelloWorld():-

It is a constructor function, constructor functions have a property that they run automatically during class execution thus used for initialization purposes and also should have the same name as the class. A tip i read in Foundation Actionscript about constructors was that  to enhance the efficiency of code  constructors should be limited to minimum amount of statements.  So here’s the trick instead of straight writing statements into  constructor function use it to call other functions, and put statements inside them.

Trace:- 

Trace gives output to output screen of your flash authoring tool.  It’s like printf of  C (not actually, just to give you a hint).  To user (in swf file) it shows up nothing so a better HelloWorld program can be :-

package {

import flash.display.MovieClip;
import flash.text.TextField;

public class HelloWorld extends MovieClip {

public function HelloWorld() {
var txtFld:TextField = new TextField();
addChild(txtFld);

txtFld.text = “Hello World!”;
}

}
}

Document Class :-

What all we did is a document class, it replaces the main timeline with code from the class given, to add a document class to your fla file you go to properties and under the class field write it’s name.

Advertisements

Leave a Reply

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

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 )

Google+ photo

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

Connecting to %s

What’s this?

You are currently reading Hello World Program / Document Class at The Flash Blog.

meta

%d bloggers like this: