Response
These are previously sorted, now sent up, hoping to help the novice ... Responding to keyboard methods as an important part of as, in today's more and more widely used, especially in flash game production, if the lack of response to the keyboard method, it is not possible, and the response to the keyboard method is the main four kinds, respectively:
1, use the button to detect
2, the use of key objects
3, using the keyboard to listen to the method
4, the use of film clips KeyUp and KeyDown events to achieve response to the keyboard
Only by mastering these methods, and then adapting, will get a lot of unexpected effects, I will combine the theory and the idea of their own brief introduction.
The first way to respond to the keyboard: Use the button to detect the response to the keyboard in the button on the event handler can not only respond to mouse events, but also to the keyboard event to respond. If you add the code shown below in the action panel of the button, you will be prompted in the Output window when tapping the X key on the keyboard: X is pressed
On the button, add:
On (keyPress "x") {Trace (' x is pressed ');}
Note, however, that when you detect the letter keys on the keyboard, the letters should be lowercase. If you want to detect special keys in the keyboard, Flash has some special code to represent them, the following list of some commonly used function key representation code:
<Left> <Right> <Up> <Down> <space <Home> <End> <Insert> <PageUp> <PageDown><Enter> <Delete> <Backspace> <Tab> <Escape>
To detect the <Left> keys on your keyboard, you can use the following actionscript:
On (keyPress "<Left>") {Trace (' Left is pressed ');}
Alternatively, you can add several on functions to a button, or you can combine multiple events in an on function, which allows you to define your own familiar shortcut keys for the button, as follows:
On (release, keyPress "<Left>") {_root.mymc.prevframe ();} On (release, keyPress "<Right>") {_root.mymc.nextframe ();}
The first statement above implements clicking a button or pressing the LEFT ARROW key on the keyboard to control the movie clip MYMC back 1 frames, and the second statement on the above implements clicking the button or pressing the RIGHT ARROW key on the keyboard to control the movie clip MYMC forward 1 frames.
Finally give the example of the original file: Click here to download the source file
Method Two
The second way to respond to the keyboard: using the Key object to implement the response to the keyboard
The use of buttons to detect key movements is very effective, but it is not conducive to the detection of persistent keys, so it is not suitable for the production of some keyboard control game.
At this point, you need to use the key object. The key object is contained under the Objects/Movies directory in the Action panel, which consists of a series of methods, constants, and functions built into the flash. Use the Key object to detect whether a key is being pressed, and if you want to detect whether the left ARROW key is being pressed, you can use ActionScript as follows:
if (Key.isdown (Key.left)) {trace ("The left Arrow was down");}
Function Key.isdown Returns a Boolean value that returns True if the key of the parameter in the number is pressed, or false. The constant key.left represents the left ARROW key on the keyboard. The function returns True when the left ARROW key is pressed.
The constants in the key object represent the corresponding keys on the keyboard, and some basic constants are listed below:
The representation of some function keys:
Key.backspace Key.enter Key.pgdn
Key.capslock Key.escape Key.right
Key.control Key.home Key.shift
Key.deletekey Key.insert Key.space
Key.down Key.left Key.tab
Key.end Key.pgup Key.up
These are the function keys on the keyboard, so how do you represent the letter keys on the keyboard?
The Key object provides a function Key.getcode to implement this function, as follows:
if (Key.isdown (Key.getcode ("x")) {Trace ("x is pressed");}
The above script means using the Key.getcode function to tell the system whether you pressed the X key, and if the X key is pressed, the function Key.isdown returns True and the x is pressed in the Output window.
Finally give the example of the original file: Click here to download the source file
Method Three
The third way to respond to the keyboard is to use the keyboard to listen to the response keyboard (personal habits in this way)
Suppose you detect a key action in the Onclipevent (Enterframe) event handler for a movie clip, and the movie clip has a longer timeline, or a slower computer operation, It is possible that when a key is pressed on a keyboard before it can handle the onclipevent (enterframe) function, the key action will be ignored, so that many of the effects you want will not be realized.
In addition, there is another problem that needs to be solved in some games (such as shooting), we need to press the key once to perform an action (firing a bullet), even if a long time to hold down a key can only count as a key, and the key object can not be different is a long time to hold down the same key or a quick number of keystrokes.
So if you want to solve this problem, you need to use the keyboard listening method. You can use the Listener (listener) to listen for keystrokes on your keyboard.
To use a listener, you first need to create it, and you can tell the computer that you need to listen for an event by using the command shown below:
Key.addlistener (_root);
The Key.addlistener command takes the main timeline or a movie clip as its parameters, and when the event that is listening occurs, you can respond to the event with the object specified by this parameter.
The code above specifies the primary timeline to respond to the event. To have the main timeline respond to the event, you also need to set up an appropriate event handler, otherwise there is no point in setting up the listener.
There are two event-handling functions for the keyboard listening: OnKeyUp and onkeydown, as follows:
Key.addlistener (_root); _root.onkeyup = function () {Trace (Key.getascii ());
The code means that when a key is pressed and released, the Output window will output the ASCII code of the key you pressed.
Of course, you can also use a movie clip as the object to listen to the keyboard, just use the path of the movie clip instead of _root as the Key.addlistener command parameter.
For example, the following code:
Key.addlistener (_ROOT.MC); _root.mc.onkeyup = function () {Trace (Key.getascii ());
The code means that when a key is pressed and released, the Output window will output the ASCII code of the key you pressed, which means the same, but the keyboard listens for different objects, one is the video MC, the other is the main timeline.
Finally give the example of the original file: Click here to download the source file
Method Four
Fourth way to respond to the keyboard: using the KeyUp and KeyDown events of the movie clips to respond to the keyboard
The last one is easy to ignore, but it also has a certain application value, the most important thing is to understand the concept.
The movie clip contains two keyboard-related events KeyUp and KeyDown that can also be used to respond to key events
For example, the following code:
Onclipevent (KeyDown) {Trace (Key.getascii ());}
When you press a key on the keyboard, the Output window outputs the ASCII value of the key that is pressed.
function Key.getascii represents the return of the ASCII code corresponding to the key, where the ASCII code is an integer, and each character on the keyboard corresponds to an ASCII code, such as the ASCII code corresponding to the letter a 65,b corresponding ASCII code for the 66,a corresponds to the ASCII code 97, b corresponds to the ASCII code of 98,+ corresponding to the ASCII code 43 and so on. Note that only the word keys has ASCII code, and the function keys on the keyboard are not ASCII.
What if I want to output a character that corresponds to a key in the Output window?
At this point, you can use the fromCharCode function of the string object to convert the ASCII code to a character, such as by changing the code of the previous example to the following:
Onclipevent (KeyDown) {Trace (String.fromCharCode (Key.getascii ()));
This means that when you press a key on the keyboard, the output of the button corresponding to the character, of course, except the function key.
For a detailed explanation of the string object, you can look under the objects/cores directory of the Actions panel.
Finally give the example of the original file: Click here to download the source file