Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Getting started with ActionScript > Running subsequent examples | |||
Now that you've developed and run the "Hello World" ActionScript 3.0 application, you should have the basic knowledge you need to run the other code examples presented in this manual.
As you're working through this manual, you may want to try out the example code listings that are used to illustrate the various topics. That testing may involve displaying the value of variables at certain points in the program, or it may involve viewing or interacting with on-screen content. For testing visual content or interaction, the necessary elements will be described before or within the code listing--you'll just need to create a document with the elements as described in order to test the code. In the case where you want to view the value of a variable at a given point in the program, there are a few ways you can accomplish this. One way is to use a debugger, such as the ones built into Flex Builder and Flash. For simple testing, however, it might be easiest to just print variable values out to some place where you can view them.
The following steps will help you create a Flash document that you can use for testing a code listing and viewing variable values:
appendText() method to the example code (described below).trace() function call to the code listing (as described below) to view the results of the example.trace() function call or add a value to the text field using its appendText() method.Since this approach is for viewing values of variables, there are two ways that you can easily view the values of variables as you're trying out the examples: write values into a text field instance on the Stage, or use the trace() function to print values to the Output panel.
trace() function: The ActionScript trace() function writes the values of any parameters passed to it (either variables or literal expressions) to the Output panel. Many of the example listings in this manual already include a trace() function call, so for those listings you'll only need to copy the code into your document and test the project. If you want to use trace() to test the value of a variable in a code listing that doesn't already include it, just add a trace() call to the code listing, passing the variable as a parameter. For example, if you encountered a code listing such as this one in the chapter,var albumName:String = "Three for the money";
you could copy the code into the Actions panel, then add a call to the trace() function such as this one to test the outcome of the code listing:
var albumName:String = "Three for the money";
trace("albumName =", albumName);
When you run the program, this line will print out:
albumName = Three for the money
Each trace() function call can take multiple parameters, which are all strung together as a single printed line. A line break is added to the end of each trace() function call, so separate trace() calls print out on separate lines.
trace() function, you can add a Dynamic text field to the Stage using the Text tool, and write out values to that text field to view the results of a code listing. The appendText() method of the TextField class can be used to add a String value to the end of the text field's contents. To access the text field using ActionScript, you must give it an instance name in the Property inspector. For instance, if your text field has the instance name outputText, the following code could be used to check the value of the variable albumName:
var albumName:String = "Three for the money";
outputText.appendText("albumName = ");
outputText.appendText(albumName);
This code would write the following text to the text field named outputText:
albumName = Three for the money
As the example shows, the appendText() method will add the text to the same line as the previous contents, so multiple values can be added to the same line of text using multiple appendText() calls. To force the text to the next line you can append a newline character ("\n"):
outputText.appendText("\n"); // adds a line break to the text field
Unlike the trace() function, the appendText() method only accepts one value as a parameter. That value must be a string (either a String instance or a string literal). To print out the value of a non-string variable, you must first convert the value to a String. The easiest way to do that is to call the object's toString() method:
var albumYear:int = 1999;
outputText.appendText("albumYear = ");
outputText.appendText(albumYear.toString());
Like this chapter, most chapters in this manual include a significant end-of-chapter example that ties together many of the concepts discussed in the chapter. However, unlike the Hello World example in this chapter, those examples will not be presented in a step-by-step tutorial format. The relevant ActionScript 3.0 code in each example will be highlighted and discussed, but instructions about running the examples in specific development environments won't be provided. However, the example files distributed with this manual will include all of the files you need to compile and run the examples easily in your chosen development environment.
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000036.html
Comments
jmmp2006 said on Apr 23, 2008 at 4:30 AM : jmmp2006 said on Apr 29, 2008 at 2:15 AM :