Download presentation
Presentation is loading. Please wait.
1
Events in Action script
2
eventTarget. addEventListener(EventType
eventTarget.addEventListener(EventType.EVENT_NAME, eventHandlerFunction); function eventHandlerFunction(eventObject:EventType):void { // Actions performed in response to the event go here. } This code does two things: It defines a function, which is the way to specify the actions that will be performed in response to the event.It calls the addEventListener() 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. 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.
3
Keyboard Event To listen Keyboard Command Keyboard Event require.
You need to use the addEventListener()method to register with a KeyboardEvent. Event is usually registered with the stage. Keyboard Event has 2 constants: 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.
4
function myKeyDown(e:KeyboardEvent):void{ trace("Key Pressed"); }
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); function myKeyDown(e:KeyboardEvent):void{ trace("Key Pressed"); } stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp); function myKeyUp(e:KeyboardEvent):void{ trace("Key release");
5
Create Practical Example
if you press any key of your Keyboard Your Movie clip is stop. if you release any key of your Keyboard Your Movie clip is start.
6
function myKeyDown(e:KeyboardEvent):void{ mc.stop(); }
In clip layer make 5 frames which contain 1 to 5 number. And in action layer. stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown); function myKeyDown(e:KeyboardEvent):void{ mc.stop(); } stage.addEventListener(KeyboardEvent.KEY_UP, myKeyUp); function myKeyUp(e:KeyboardEvent):void{ mc.play();
7
two properties that belong the Keyboard Event Class:
keyCode - The key code is a numeric value representing the position of the key of the keyboard. charCode - 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 keyCode but different character codes.
8
keycode for keyboard key
Left Arrow Key 37 Right Arrow Key 39 Up Arrow Key 38 Down Arrow Key 40
10
function reportKeyDown(ke:KeyboardEvent):void { trace(ke.charCode );
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown); function reportKeyDown(ke:KeyboardEvent):void { trace(ke.charCode ); trace(ke.keyCode ); txt.text=“Position Value = “ +ke.keyCode; txt.text=ke.charCode.toString(); txt.text="Key Pressed: " + String.fromCharCode(ke.charCode) " (character code: " + ke.charCode + ")” " (Key code: " + ke.keyCode + ")"; }
11
Public Properties of KeyboardEvents
Public Property Data type Description altKey Boolean True if it is press charCode uint numeric value representing the character associated with the key ctrlKey shiftKey KeyCode numeric value representing the position of the key of the keyboard if (ke.ctrlKey) { txt.text=ke.keyCode.toString(); }
12
function displayKey(ke:KeyboardEvent) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, displayKey); function displayKey(ke:KeyboardEvent) { if (ke.keyCode == 38) { mc.y -=2; } else if (ke.keyCode == 40) { mc.y +=2; else if (ke.keyCode == 37) { mc.x -=2; else if (ke.keyCode == 39) { mc.x +=2;
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.