EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 148

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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” doesnt have a
break
statement, the block executes if the user presses the e or E key. If the user presses a key that
isnt caught by any of the first three cases, the default case executes.

Table of Contents

Related product manuals