Home > ActionScript 3.0 Tutorial, Flash Development > ActionScript 3.0 Hello World! Tutorial

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 ca\flashdev\hello\.
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

  1. bobby
    November 11th, 2006 at 22:57 | #1

    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. January 22nd, 2007 at 19:58 | #2

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

  3. May 28th, 2007 at 21:00 | #3

    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. June 5th, 2007 at 07:12 | #4

    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. July 19th, 2007 at 12:59 | #5

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

  6. Joir-dan
    August 6th, 2007 at 23:04 | #6

    a great help

  7. Bru
    September 29th, 2007 at 10:54 | #7

    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 Settings\Bru\My Documents\design\lab\hellouws\HelloWorld.as

    Both files are in the same place(folder)…

  8. Bru
    September 29th, 2007 at 10:58 | #8

    OK ok ok it was my mistake :-)

    Now it worked!!!

    Thanks for the tutorial!!

  9. West
    October 19th, 2007 at 07:33 | #9

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

  10. sam
    June 25th, 2008 at 11:19 | #10

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

  11. June 29th, 2008 at 09:03 | #11

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

  12. Froztwolf
    December 26th, 2009 at 17:58 | #12

    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. April 24th, 2010 at 15:56 | #13

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

  14. ddd
    May 26th, 2010 at 01:28 | #14

    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!

  1. No trackbacks yet.