User Guide
32 Getting Started with ActionScript
The Greeter class now has a number of new features:
■ The validNames array lists valid user names. The array is initialized to a list of three
names when the Greeter class is loaded.
■ The sayHello() method now accepts a user name and changes the greeting based on
some conditions. If the
userName is an empty string (""), the greeting property is set
to prompt the user for a name. If the user name is valid, the greeting becomes
"Hello,
userName.”
Finally, if either of those two conditions are not met, the greeting
variable is set to
"Sorry, userName, you are not on the list."
■ The validName() method returns true if the inputName is found in the validNames
array, and
false if it is not found. The statement validNames.indexOf(inputName)
checks each of the strings in the
validNames array against the inputName string. The
Array.indexOf() method returns the index position of the first instance of an object
in an array, or the value -1 if the object is not found in the array.
Next you will edit the Flash or Flex file that references this ActionScript class.
To modify the application using Flex Builder:
1. Open the HelloWorld.mxml file.
2. Next modify the <mx:TextArea> tag to indicate to the user that is is for display only, by
changing the background color to a light gray and preventing the user from editing the
displayed text:
<mx:TextArea id = "mainTxt" width="400" backgroundColor="#DDDDDD"
editable="false" />
3.
Now add the following lines right after the <mx:TextArea> closing tag. These lines create
a new TextInput field that lets the user enter a user name value:
<mx:HBox width="400">
<mx:Label text="User Name:"/>
<mx:TextInput id="userNameTxt" width="100%" enter="mainTxt.text =
myGreeter.sayHello(userNameTxt.text);" />
</mx:HBox>
The enter attribute specifies that when the user presses the Enter key in the userNameTxt
field, the text in the field will be passed to the
Greeter.sayHello() method, and the
greeting displayed in the
mainTxt field will change accordingly.
The final contents of the HelloWorld.mxml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
layout="vertical"
creationComplete = "initApp()" >
<mx:Script>
<![CDATA[