For most people who learn how to program, the first thing they learn is to display "Hello World!" words. So today, I suggest we should learn how to display a "Hello World!" words in Macromedia flash as well.
In flash actionscript, to display texts in the output window, we use trace() command. It is one of the essential tools for debugging in the flash movies. It comes very handy when we want to know what's going on in runtime.
Let's us look at an example:
Note: for all the flash examples below, open a new flash document and on the first frame paste the flash actionscript into action panel. Press Ctrl + Enter to test the flash movie. If you do not see an Output Window display for any reason, press F2 to open it.
| trace("Hello World!"); |
This actionscript will display the phrase "Hello World!" in the output window.
How about when we want to display some dynamic variable? Try this:
| name = "your name";
trace("Hello " + name + ", welcome to CambodiaXP.com"); |
This actionscript will display the phrase "Hello your name, welcome to CambodiaXP.com" in the Output Window.
Plus ( + ) is an operator. What it does is it joins the text together. In our case, the text are "Hello ", "your name" and ", welcome to CambodiaXP.com". Note that the variable 'name' has been replaced by the text "your name".
Below is our last example. It shows you how trace() command being used as debugging tool.
| myNum = Math.random();
if (myNum<0.5) { trace("Number is less than 0.5 and the number is "+myNum); } else { trace("Number is more than 0.5 and the number is "+myNum); } |
This flash actionscript will display one of the two phrases depending on the value stored in the variable 'myNum'. For your interest, 'myNum' variable is getting the value from the Math.random() method which will generate a random floating-point number from 0 to 0.999999999.
Flash actionscript trace() command is very useful command, so I hope this tutorial will help you to kick start on more advance used of trace() flash actionscript command.


