148
Syntax and Language Fundamentals
To use a switch statement in a document:
1.
Select File > New and then select Flash Document.
2.
Select Frame 1 of the Timeline, and then type the following ActionScript in the
Actions panel:
var listenerObj:Object = new Object();
listenerObj.onKeyDown = function() {
// Use the String.fromCharCode() method to return a string.
switch (String.fromCharCode(Key.getAscii())) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
/* E doesn't have a break statement, so this block executes if you
press e or E. */
trace("you pressed E or e");
break;
case "I" :
case "i" :
trace("you pressed I or i");
break;
default :
/* If the key pressed isn’t caught by any of the above cases,
execute the default case here. */
trace("you pressed some other key");
}
};
Key.addListener(listenerObj);
3.
Select Control > Test Movie to test the ActionScript.
Type letters using the keyboard, including the a, e, or i key. When you type those three
keys, you’ll see the
trace
statements in the preceding ActionScript. The line of code
creates a new object that you use as a listener for the Key class. You use this object to
notify the
onKeyDown()
event when the user presses a key. The
Key.getAscii()
method
returns the ASCII code of the last key that the user presses or releases, so you need to use
the
String.fromCharCode()
method to return a string that contains the characters
represented by the ASCII values in the parameters. Because “E” doesn’t have a
break
statement, the block executes if the user presses the
e
or
E
key. If the user presses a key that
isn’t caught by any of the first three cases, the default case executes.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...