Robot+Events

**I. Introduction**
media type="custom" key="7697237" align="center"

**II. Learning Objectives**
//**Knowledge**// //**Skills.** By the end of the lesson you will be able to ...//
 * **Event Listeners** -- event listeners listen to any events (of a certain type) dispatched from a specific object instance (the event dispatcher). The event listener is just a function that takes an event as it's one on only input parameter. You can add (and remove) multiple event listeners to single event dispatcher.
 * Add and remove multiple event listeners to an event dispatcher.

**III. Concepts**
Events are messages passed between objects at some undetermined time. Events are a special kind of object in Flash. For now, all you need to know about them is that all events have a //type// property. An event's type is just a String specifying what kind of event it is. You need to know an event's type in order to listen for it.
 * Events**

In the simulator at the beginning of the lesson, the //stand, clap, jump,// and //stomp// signals represent the events.

You are already familiar with events in MXML. For example the "click" event is just an event whose type property is the string "click".

Some objects are able to //send// messages. In programming, this is called //dispatching events//. The event dispatcher creates a new event and calls a special dispatch message which sends it out. If there are any event listeners, they'll receive the event.
 * Event dispatchers**

In the simulator at the beginning of the lesson, the radio tower represents the event dispatcher.

You can think of an event dispatcher as a kind a mailing list owner. It types up an email (the event message) then sends it to anyone who's registered on the mailing list. It sends out the message whenever it's time, whether or not there are 100 people registered or no people registered. All event dispatchers implement two methods called addEventListener and removeEventListener.

In MXML, all the gui components are event dispatchers. For example in the the Button dispatches an event with the type "click//"//.

Some objects are able to //receive// messages. In programming, this is called //handling events//. In Flash, an event handler is just a function that takes an event as it's one and only input parameter.
 * Event Listeners**

In the simulator at the beginning of the lesson, the robots represent the event listeners.

To add an object (or rather an object's function) as an event listener to an event dispatcher, you use the addEventListener method. For example, let's say that you have an event dispatcher called _radio that dispatches four types of events with the types: "stand", "clap", "jump", and "stomp". To add a listener, you use code like:

code format="mxml" // This function can listen to an event function myHandlerFunction(anEvent:Event):void{ // do something... }

//This is how you add the function as a listener to _radio for the "stand" event _radio.addEventListener(myHandlerFunction, "stand");

code Notice that to add a function as a listener, you use the addEventListener function of the event dispatcher. You have to give the addEventListener function two parameters. The first parameter is the name of the function that will listen for the event. The second parameter is the type of the event to listen to. Note that if the event dispatcher dispatches a different kind of event, the listener function won't receive that event (//unless// you use addEventListener again to listen for the second event type).

In MXML, you can also add event listeners to an event dispatcher. For example, the  component has a "click" property which is actually an event type. When you say click="someFunction(event)" you are actually adding someFunction as an event listener to the "click" event of the button. You can add multiple event listeners in MXML by using code like: click="someFunction(event); anotherFunction(event)".

**IV. Scenario**
Here's what the program should do:

**Evil Robot Training Simulator (TM) Mark III**

**Use case** 1. The henchman specifies which events robot 1 should listen to. 2. The henchman specifies which events robot 2 should listen to. 3. The henchman broadcasts an event 4. Robot 1 & Robot 2 do the specified events

There are a few things you should know about the Radio class. Radio has the following functions:
 * broadcast(signal:String) will dispatch an event with the type given by signal

There are a few things you should know about the Robot class. Robot has the following functions:
 * stand(event:Event=null) will make the robot stand. It can take an event as a parameter or 0 parameters
 * clap(event:Event=null) will make the robot clap. It can take an event as a parameter or 0 parameters
 * stomp(event:Event=null) will make the robot stomp. It can take an event as a parameter or 0 parameters
 * jump(event:Event=null) will make the robot jump. It can take an event as a parameter or 0 parameters

There are a few things you should know about the RobotAction class. RobotAction has the static properties:
 * RobotAction.STAND is equal to the string "stand"
 * RobotAction.CLAP is equal to the string "clap"
 * RobotAction.STOMP is equal to the string "stomp"
 * RobotAction.JUMP is equal to the string "jump"

**V. Program**
code format="mxml"  

 <![CDATA[ import edu.nu.easterday.robot.*;

private function makeRadioBroadcast(signal:String):void{ _radio.broadcast(signal); }

private function listenToStand(event:Event, robot:Robot):void {               if(event.target.selected){ _radio.addEventListener(RobotAction.STAND, robot.stand); }else{ _radio.removeEventListener(RobotAction.STAND, robot.stand); }           }

private function listenToClap(event:Event, robot:Robot):void {               if(event.target.selected){ _radio.addEventListener(RobotAction.CLAP, robot.clap); }else{ _radio.removeEventListener(RobotAction.CLAP, robot.clap); }           }

private function listenToStomp(event:Event, robot:Robot):void {               if(event.target.selected){ _radio.addEventListener(RobotAction.STOMP, robot.stomp); }else{ _radio.removeEventListener(RobotAction.STOMP, robot.stomp); }           }

private function listenToJump(event:Event, robot:Robot):void {               if(event.target.selected){ _radio.addEventListener(RobotAction.JUMP, robot.jump); }else{ _radio.removeEventListener(RobotAction.JUMP, robot.jump); }           }

]]>      

   

    <s:CheckBox label="Stomp" change="listenToStomp(event, _robot1)"/> <s:CheckBox label="Jump" change="listenToJump(event, _robot1)"/> </s:VGroup> <s:VGroup x="469" y="177" width="63" height="89" gap="0"> <s:Label text="Listen for:"/> <s:CheckBox label="Stand" change="listenToStand(event, _robot2)"/> <s:CheckBox label="Clap" change="listenToClap(event, _robot2)"/> <s:CheckBox label="Stomp" change="listenToStomp(event, _robot2)"/> <s:CheckBox label="Jump" change="listenToJump(event, _robot2)"/> </s:VGroup> </s:Application>

code

**VI. Self-explain (Try it!)**
Go over each line program and explain out loud to yourself what it does. If you don't know exactly what the tag does, try to guess.

**VII. Program Walkthrough (Try it!)**
To create the program, do the following:
 * Create a flex project
 * Download the [[file:Robot.swc]] and [[file:Radio.swc]] libraries and put them in the libs directory
 * Create the interface
 * Create event listeners for the radio tower buttons. The event listener should make the radio dispatch an event. Use RobotAction for the event types.
 * Create event listeners for the robot check boxes. The event listeners should add or remove the robots as event listeners to the radio for the given events