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).
This is real starter which will let an AS 2.0 programmer(a beginner) could shift to AS 3.0 with ease.
Thank you
Right on! Thanks. I’m also trying to switch from 2.0 to 3.0 and this helped.
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.
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.
Nice, easy tutorial, helped me into learning flash for the first time.
a great help
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)…
OK ok ok it was my mistake
Now it worked!!!
Thanks for the tutorial!!
I cannot us the folder “includes.as.temp”
because of de “.as”. Weird not? Because it could be an extension?
How do I get it to include code completion for classes that I create?
Sam, if you use Eclipse with the Flex Builder 3 plugin, code completion should be automatic.
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!
Enjoyed the actionscript 3.0 tutorial post. Not many blogs worth bookmarking but this is going on Delicious now!
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!