Main Content

You are here:

Hello World!

Hello and welcome. Since writing this tutorial it has been split into two parts, this one which has what you need to know to get on with, and another page regarding the function declaration.

Today we will be looking at a "simple" Hello trace; it is a little more complicated than it needs to be, maybe, but it should show you how to run simple parts of ActionScript 3.0. The secret in this is preparation.

Your first step is to make a new project, with one Flash Document, one ActionScript File. We shall start with the ActionScript file, which should be saved as Doorman.as. The first line will be:

package{

That will always start ActionScript files, but all it means is that it packages loads multiple classes. Then comes the defining of those. A class is a kind of template, a blueprint if you will. The items you make from this blueprint are called objects, which you can create from the class. Each class (and hence any object created from it) can have methods (basically functions) within it.

It might help to think of the real world. Suppose you wanted to design a car, so you had some blueprints drawn up, by a designer, of your car. The class would be the plan, the cars you created from that plan objects and then the methods that each car had would be "open_door()" or "engine_on()". Following? It should become clear(er) soon.

     public class Doorman{

"Doorman" is now the name of our class. The "public" before it means that the .fla can access it. So we have our package, and our class, so now we need functions. What does a doorman do? Say hello of course (among many other things)!

           public function sayHello(nameof:String):String{

An in depth description of that line can be found on a separate page. To summarise: when you use a function, you input arguments (in this function a string called nameof), and the function outputs the return value (another string).

The remaining section is, thankfully, a tad simpler:

                     var greeting:String = "Hello "+nameof;
                        return greeting;
                }
        }
}

We make a new string variable called greeting, make it equal to Hello and the name combined and then return it, plus closing all of the open brackets. Now we are on the home straight. Save and go to your Flash document. Go to the first slide (probably best to create an actions layer), go to the actions panel and type:

import Doorman;
var Andy:Doorman = new Doorman();
trace(Andy.sayHello('World!!!'));

The first line imports (enables us to use) the class we made. This is because the class was called Doorman and was in the file called Doorman.as.

The second line makes a new doorman called Andy. Andy is an object based on the calls "Doorman".

The third line gets him to say hello to "World!!!" The function is called and returns the string "Hello World!!!", which is then traced. Test the project, and watch the output screen.

This is a great base of information, most of which need not change later, so reread this and remember it. Next time we will look at creating a nice circle on the stage, and then controlling it (in a later tutorial).

Harry.

Comments