ActionScript3:Hello World

From virtualMV2015wiki
Revision as of 16:01, 14 January 2015 by Hesperides (Talk | contribs) (1 revision imported)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
 Home  Flash11 (CS5) Flash10 (CS4) <

Using Flash 10 (CS4) and ActionScript 3.0

Objectives

  • To introduce Scripting, Classes and objects.
  • To demonstrate the use of a trace as a debugging option
  • To create a display object on the stage using ActionScript 3.0
  • To create an external ActionScript 3.0 class file

References: Rosenzweig (2008)[1]

Using trace

Based on Rosenzweig (2008, pp.9-10)

  • Create New > Flash File (ActionScript 3.0)
  • Right click on Layer1 / frame 1 and choose Actions (or single click on the frame then [F9])

Create new flash AS 3 file Choose Layer1, frame 1

  • Type in
trace{"Hello World");
  • Close the window (you actually don’t really have to - but the output pane may be hidden behind it!)
  • To test it, type [Ctrl]+[Enter], and output pane is displayed:

Using trace to display hello world

  • Notes
    • The flash movie stage does not show anything (as we are using a debugging command) so the results are displayed in a debugging "output" window.
    • This will only work while testing the movie.. so onto the next example…

Using a display object

Based on Rosenzweig (2008, pp12-13)

Create a new movie with the following ActionScript

// create a display object called txtComment
var txtComment:TextField = new TextField();
// put Hello world into the display object
txtComment.text="Hello World";
//add the display object to the stage
addChild(txtComment);
trace{"Hello World");
  • Then run the movie ([Crtl]+[Enter], a little “Hello World” appears on the stage

Hello World using a display object

  • Notes
    • addChild adds the txtComment object to the stage
    • The stage size is set in the movies properties, so if you resize it the Hello World” may disappear! (I have set my stage size to 250x250 to show all the things on one screen shot)
    • The position of “Hello World” can be set through the code.

Using an external class

Based on Rosenzweig (2008, pp13-15)

  • Create a new external ActionScript File: File > New > ActionScript File

Create a new external ActionScript File:

  • .. and enter the following (Note the class, function and filename all need to be the same)
//start class file with package
package {
	// include classes to display objects on the stage and work with text
	import flash.display.*;
	import flash.text.*;
//define the class and tell it to work with a movieclip (the stage)
	public class HelloWorld3 extends MovieClip {
		// create a function with the same name as the class so
		// when the class is initialised it will run immediately
		// called a constructor class
		public function HelloWorld3 () {
			var txtComment:TextField=new TextField  ;
			txtComment.text="Hello World";
			addChild(txtComment);
 		}
	}
}
  • Use the tick to check the syntax (top left)
  • Save the file as HelloWorld3.as

Include the class into a flash movie

  • Create new Flash File (ActionScript 3.0)
  • Save the movie HelloWorld3.fla (otherwise it won't be able to figure out how to link the 'as' file)
  • Assign the document class to the movie in the properties pane (our ActionScript file: HelloWorld3)
  • Then run the movie

Fla File with external (as) file attached

  • Notes:
    • A class can be though of as the code part of the object and defines what data is associated and what functions it can perform.

Troubleshooting

  • The code cannot be pasted directly from this page (as it contains formatting)
  • Reminder that case is significant (e.g. txtComment is not the same as txtcomment )
  • For an external class the class, function and filename all need to be the same


Icon References.png References

  1. Rosenzweig, G. (2008) ActionScript 3.0: Game Programming University. Que. ISBN: 0-7897-3702-1.

ActionScript3:Hello World. (2017). In virtualMV's ( Michael Verhaart ) wiki. Retrieved December 14, 2017, from http://www.virtualmv.com/wiki/index.php?title=ActionScript3%3AHello_World    (zotero)