|
EVENT LISTENERS Event listeners are added to objects, so they can hang out and wait around until something happens. For example, you can add an event listener to a button so it responds to the users mouse actions. Two Parts 1) The addEventListener() method instructs the object to wait for a particular event. 2) The event-handler function is what is called when the event happens. personOnSideWalk.addEventListener(RainEvent.ON_RAIN, umbrella); function umbrella():void { The event listener will continue to wait for the event until it is removed. Here is an example for a button: myButton1.addEventListener(MouseEvent.ROLL_OVER, rolly); function rolly(event:MouseEvent):void { The first element in the parenthesis is the event the object will wait for. Here we are waiting for a mouse event of the rollover. Notice that event names are all in upper case with underscores for spaces between words. (Other MouseEvents are ROLL_OVER, ROLL_OUT and CLICK) A comma separates the event from the event-handler function, which is called when the event occurs. Notice that the event-handler function refers to the event that triggered it in parenthesis, an event in the form of a MouseEvent. The colon followed by the word void means the function will not return any data. Curly braces wrap and contain the code to be executed together like a nice, warm tortilla shell! MouseEvents: MouseEvent.CLICK // User clicks on object MouseEvent.ROLL_OVER // User rolls over object MouseEvent.ROLL_OUT // User rolls off of object MouseEvent.MOUSE_MOVE // When user moves mouse anywhere in the active area of an object MouseEvent.DOUBLE_CLICK // User clicks twice in rapid succession, doubleClickEnabled property of object must be set to true. Example: myButton1. doubleClickEnabled=true;
|