4.5.09

AS3: Hello World Movie

First AS3 Movie: Hello World

This is a guide to making a quick Hello World app in FlashDevelop with ActionScript 3. This tutorial assumes you have FlashDevelop up and running, and a default ActionScript 3 project open, ready to build and run. If not check this tutorial for instructions on how to get there.

Building on the last tutorial, this is a very simple addition.



   // entry point
var textField:TextField = new TextField(); // Create a new TextField object
textField.text = "Hello World"; // Assign the text property of the field
addChild(textField); // Add the TextField to this Sprite

Breaking it down line by line:



  • Declaring and assigning a TextField:
       var textField:TextField = new TextField(); // Create a new TextField object
    This statement declares a TextField type variable named textField. The syntax for declaring a variable is: var variableName:variableType; Declaring a variable just tells the program to prepare for the existence of an object. If you try to do anything with an unassigned variable other than assign it to something, or check if the variable is null, you will get some kind of null object error. The way the variable is assigned above is just for convenience, it could also be done using:
       var textField:TextField; // Declare a new TextField variable
    textField = new TextField(); // Assign the variable to an object


  • Setting the text to be shown:
    textField.text = "Hello World";    // Assign the text property of the field

    This statement assigns the "text" property of the textField object to "Hello World". The "text" field contains the string that the TextField will draw to the stage. "Hello World" is surrounded by double quotes, which makes it a string in AS3.

  • Adding the TextField object to be drawn:
       addChild(textField);      // Add the TextField to this Sprite
    addChild(parameter) is a method that makes the object you used as a parameter a child of the drawable object that used the method. This means that textField is now a child of Main, and when Main draws, so will TextField (unless it's "visible" property is set to false). The class you are using extends the class "Sprite" so although you don't see the function addChild() defined anywhere in your code, it is a member of your class through its superclass.

  • Note:If you copy this instead of typing it in and using the autocomplete feature you will have to add this line up by your other imports to use the TextField:
    import flash.text.TextField;

Now build your project and run, you should now see the words "Hello World" in the top left corner of your window.

.zip source code for this project can be found here.

Tutorials on the Basics:
Embedding an Image
Dynamically loading an Image
Embedding and Playing Sound
Dynamically loading and Playing Sound

No comments:

Post a Comment