1 / 11

Event Handling in flash

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

Download Presentation

Event Handling in flash

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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

  3. 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() {}

  4. 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() { … }

  5. 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)

  6. 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

  7. 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

  8. 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:

  9. 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…)

  10. 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

  11. Excersice • Write a flash application that moves a movie clip symbol using Keyboard.

More Related