ActionScript 3.0 Hello World! Tutorial

When learning any new programming language the first exercise is always…
you guessed it! Hello World!.

Open a text editor, I use SEPY for coding ActionScript, and type the following code:

package ca.flashdev.hello {
  public class HelloWorld {
    public function sayHello():String {
      var greeting:String = "Hello World!";
      return greeting;
    }
  }
}

Save this file as HelloWorld.as. Place it in a folder structure caflashdevhello.
If you already code in ActionScript 2 you will notice the addition of package to the top of my code. When you use OOP you place your class files in a folder structures. Each folder is considered a package. When you import classes into your code you have to option of importing an entire folder of classes using the * symbol (e.g. import ca.flashdev.utils.*). Packages let us organize classes into logical groups and also prevent naming conflicts between classes. Now in ActionScript 3 you put the package name (the folder name that this class file is contained inside) in the top of you class file.

The next step is to open Adobe Flash 9 Public Alpha. File -> New -> Flash Document. Select the Text Tool (t) and create a dynamic text field on the stage with an instance name of hello_txt. Click Embed in the properties panel and select Uppercase, Lowercase and Punctuation. Create another Layer in the Timeline and name it actions. Click on the new blank frame in the Timeline, open your Actions (F9) panel and add the following code:

import ca.flashdev.hello.HelloWorld;
var classInstance:HelloWorld = new HelloWorld();

hello_txt.text = classInstance.sayHello();

Save this file in the same folder as your ca folder and name it helloWorld.fla. Now publish the Movie (Ctrl-Enter). The text field you added should display Hello World!.

Make sure you have ActionScript 3 selected in the ActionScript Version field of your Publish Settings (Ctrl-Shift-F12).

Download the Source Files

15 thoughts on “ActionScript 3.0 Hello World! Tutorial

  1. bobby says:

    This is real starter which will let an AS 2.0 programmer(a beginner) could shift to AS 3.0 with ease.
    Thank you

  2. Vernon says:

    Right on! Thanks. I’m also trying to switch from 2.0 to 3.0 and this helped.

  3. rory says:

    I have never programmed action script before, and this was the best beginner tutorial to get my feet wet in action script 3.0. Thanks a lot. I’m rolling now.

  4. Daniel says:

    I’ve never really used Flash or ActionScript and this was reasonably good to follow. Well done to Fraser for a cool Tutorial, and well done to Adobe for making Flash reasonably simple to use.

  5. James says:

    Nice, easy tutorial, helped me into learning flash for the first time.

  6. Joir-dan says:

    a great help

  7. Bru says:

    I got this:

    5001: The name of package ‘ca.flashdev.hello’ does not reflect the location of this file. Please change the package definition’s name inside this file, or move the file. C:Documents and SettingsBruMy DocumentsdesignlabhellouwsHelloWorld.as

    Both files are in the same place(folder)…

  8. Bru says:

    OK ok ok it was my mistake :-)

    Now it worked!!!

    Thanks for the tutorial!!

  9. West says:

    I cannot us the folder “includes.as.temp”
    because of de “.as”. Weird not? Because it could be an extension?

  10. sam says:

    How do I get it to include code completion for classes that I create?

  11. Sam, if you use Eclipse with the Flex Builder 3 plugin, code completion should be automatic.

  12. Froztwolf says:

    I had some trouble getting this to work at first because I forgot to set the instance name. If you are trying this and nothing happens see if you are having the same problem. Thanks for the article!

  13. Enjoyed the actionscript 3.0 tutorial post. Not many blogs worth bookmarking but this is going on Delicious now!

  14. ddd says:

    I had some trouble getting this to work at first because I forgot to set the instance name. If you are trying this and nothing happens see if you are having the same problem. Thanks for the article!

  15. Toni says:

    Thanks for the awesome tutorial mate, it’s just what i was looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>