120 likes | 252 Views
Event Handling in flash. Event: is an instantaneous occurrence usually triggered by user. Ex) clicking a button, moving mouse, clicking on a mouse Event Handling: writing action script that will be executed when the event occurs. Events can be handled by using Callback
E N D
Event Handling in flash • Event: is an instantaneous occurrence usually triggered by user. • Ex) clicking a button, moving mouse, clicking on a mouse • Event Handling: writing action script that will be executed when the event occurs. • Events can be handled by using Callback • Callback: function tied directly to a particular event.
Event Handling in flash • Ex) onEnterFrame = function() { …. } • Event handling examples: • Handling button events: • onPress: the event is triggered when a button is pressed. • ex) btn.onPress = function() { …. } Code written inside function is tied to the event (onEnterFrame) • Code written here will be executed when (btn) is pressed • This callback function is written in the first frame btn: is the instance name of a button
Event Handling in flash • onRelease: The event is triggered when a button is released after being pressed. • ex) btn.onRelease = function() {…} • onRollOver: The event is triggered when the mouse cursor moves over a button. • Ex) btn.onRollOver = function() {…} • Handling mouse events: • onMouseDown: it is triggered whenever the mouse button is pressed anywhere in the stage ex) onMouseDown = function() {}
Event Handling in flash • onMouseUp: it is triggered whenever the mouse button is released after being pressed ex) onMouseUp=function() { … } • onMouseMove: it is triggered whenever the mouse cursor moves over a component ex) onMouseOver=function() { … }
Mouse Events Application • Ex) This example shows how to drag a movie clip symbol from place to another using mouse events. • Create a new flash document • Insert a movie clip symbol onto the stage • Give it an instance name (m_shape) • In the action panel of the first frame: • Declare a number variable (i) and initialize it to zero • Add a (onMouseDown) handler that assigns one to (i)
Mouse Events Application • Add a (onMouseMove) handler and insert the following code into it: if(i==1) { m_shape._x=_xmouse; m_shape._y=_ymouse; } • Add a (onMouseUp) handler that assigns zero to (i) • Test the Program To move the movie clip symbol with the mouse cursor To check whether the mouse button is being pressed
Handling Key Events • Key events are handled by • Creating an object to listen to key events • Associate the object with the key event types • Register the object to start listening to key events • Event types triggered when pressing keyboard keys: • onKeyDown: it is triggered whenever a keyboard button is clicked down • onKeyUp: it is triggered whenever a keyboard button is released
Handling Key Events • Ex) This example receives events from keyboard and prints the code of the pressed key: • Create a new flash document • In action panel of the first frame, type the following code:
Handling Key Events • Some important functions found in class key: • Key.getAscii(): returns the ASCII code of the pressed alphanumeric character (s,r,5,3,….) • Key.getCode(): returns the code of the pressed non-alphanumeric keys (shift, ctrl, tab…)
Handling Key Events var i:Object = new Object(); i.onKeyDown = function() { trace(Key.getCode()); } Key.addListener(i); Creating an object to listen to key events Associate the object with the key event types This code will be executed when pressing on any keyboard key Register the object to start listening to key events
Excersice • Write a flash application that moves a movie clip symbol using Keyboard.