Events in Action script event Target add Event

  • Slides: 13
Download presentation
Events in Action script

Events in Action script

event. Target. add. Event. Listener(Event. Type. EVENT_NAME, event. Handler. Function); function event. Handler. Function(event.

event. Target. add. Event. Listener(Event. Type. EVENT_NAME, event. Handler. Function); function event. Handler. Function(event. Object: Event. Type): void { // Actions performed in response to the event go here. } This code does two things: l It defines a function, which is the way to specify the actions that will be performed in response to the event. It calls the add. Event. Listener() method of the source object, in essence "subscribing" the function to the specified event so that when the event happens, the function's actions are carried out. l When the event actually happens, the event target checks its list of all the functions and methods that are registered as event listeners. It then calls each one in turn, passing the event object as a parameter.

Keyboard Event To listen Keyboard Command Keyboard Event require. l You need to use

Keyboard Event To listen Keyboard Command Keyboard Event require. l You need to use the add. Event. Listener()method to register with a Keyboard. Event. l Event is usually registered with the stage. l Keyboard Event has 2 constants: l l l KEY_DOWN : - it is possible to trigger keyboard events KEY_UP : - is triggered when you release the key and not when you press it down.

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, my. Key. Down); function my. Key. Down(e: Keyboard.

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, my. Key. Down); function my. Key. Down(e: Keyboard. Event): void{ trace("Key Pressed"); } stage. add. Event. Listener(Keyboard. Event. KEY_UP, my. Key. Up); function my. Key. Up(e: Keyboard. Event): void{ trace("Key release"); }

l Create Practical Example if you press any key of your Keyboard Your Movie

l Create Practical Example if you press any key of your Keyboard Your Movie clip is stop. l if you release any key of your Keyboard Your Movie clip is start. l

In clip layer make 5 frames which contain 1 to 5 number. l And

In clip layer make 5 frames which contain 1 to 5 number. l And in action layer. stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, my. Key. Down); l function my. Key. Down(e: Keyboard. Event): void{ mc. stop(); } stage. add. Event. Listener(Keyboard. Event. KEY_UP, my. Key. Up); function my. Key. Up(e: Keyboard. Event): void{ mc. play(); }

l two properties that belong the Keyboard Event Class: l l l key. Code

l two properties that belong the Keyboard Event Class: l l l key. Code - The key code is a numeric value representing the position of the keyboard. char. Code - The character code is a numeric value representing the character associated with the key. The difference between these two becomes apparent when you realize that small and capital "A" letters have the same key. Code but different character codes.

keycode for keyboard key l Left l Arrow Key 37 l Right l 39

keycode for keyboard key l Left l Arrow Key 37 l Right l 39 l Up l Arrow Key 38 l Down l Arrow Key 40 Arrow Key

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, report. Key. Down); function report. Key. Down(ke: Keyboard.

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, report. Key. Down); function report. Key. Down(ke: Keyboard. Event): void { trace(ke. char. Code ); trace(ke. key. Code ); txt. text=“Position Value = “ +ke. key. Code; txt. text=ke. char. Code. to. String(); txt. text="Key Pressed: " + String. from. Char. Code(ke. char. Code) + " (character code: " + ke. char. Code + ")” + " (Key code: " + ke. key. Code + ")"; }

Public Properties of Keyboard. Events Public Property Data type Description alt. Key Boolean True

Public Properties of Keyboard. Events Public Property Data type Description alt. Key Boolean True if it is press char. Code uint numeric value representing the character associated with the key ctrl. Key Boolean True if it is press shift. Key Boolean True if it is press Key. Code uint numeric value representing the position of the keyboard if (ke. ctrl. Key) { txt. text=ke. key. Code. to. String(); }

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, display. Key); function display. Key(ke: Keyboard. Event) {

stage. add. Event. Listener(Keyboard. Event. KEY_DOWN, display. Key); function display. Key(ke: Keyboard. Event) { if (ke. key. Code == 38) { mc. y -=2; } else if (ke. key. Code == 40) { mc. y +=2; } else if (ke. key. Code == 37) { mc. x -=2; } else if (ke. key. Code == 39) { mc. x +=2; } }

l http: //www. flashandmath. com/flashcs 5/p eekweb/ l http: //www. freeactionscript. com/page/5/

l http: //www. flashandmath. com/flashcs 5/p eekweb/ l http: //www. freeactionscript. com/page/5/