1 / 20

Flash Event Listener: Button Event Demonstration

This tutorial demonstrates how to use event listeners in Flash to respond to button events.

jlorene
Download Presentation

Flash Event Listener: Button Event Demonstration

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. Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;

  2. Functions function myFunctionName() { //instructions go here }

  3. Functions that accept parameter values function myFunctionName(param:DataType) { trace(param); }

  4. Functions that return values function myFunctionName():ReturnDataType { return someValue; }

  5. Example function that accepts paramters and returns a value function generate(minRange:Number, maxRange:Number):uint { returnMath.floor(Math.random() * (maxRange - minRange) + minRange); } var num:uint = generate(5, 10); trace(num); //generates random number between 5 and 10

  6. Embedding fonts into textboxes When you set a textbox to a dynamic type so it can have an instance name for dynamic text, you must also embed the fonts. Demonstrate refresher of creating dynamic textbox.

  7. ActionScript 3’s Event Listener Model ActionScript 3 is driven by the event listener model to notify the Flash Virtual Machine of whenever an important event occurs. Certain types of objects have the ability to broadcast when some event takes place so subsequent instructions may be executed when that exact event takes place. Assigning event listeners to listen to event broadcasts is optional.

  8. ActionScript 3’s Event Listener Model • Event Scenario: • You’re driving somewhere. As long as your car doesn’t break down, you have no reason to call CAA for any road support or towing services. • In the event that your car breaks down, you have to do two things for them to successfully help you: • first, you have to call a CAA operator and notify them that your car broke down. • then, you have to give instructions to the CAA operator of where it broke down so they can find you.

  9. Events are different actions an object can be doing.

  10. Event Model Syntax First, we have to assign an event listener to listen to an object that might or might not broadcast an event if you want to listen for something. myButton.addEventListener(); Then, we have to tell the event listener what to specifically listen for. We’re not just going to respond to any random event that might occur. myButton.addEventListener(“mouseClick”); Event listener types are string values;

  11. Event Model Syntax Each individual event listener can listen to just one event at a time. You can have multiple event listeners assigned to one object to listen for multiple types of events. myButton.addEventListener(“mouseClick”); myButton.addEventListener(“rollOver”); myButton.addEventListener(“rollOut”);

  12. Event Model Syntax There’s no point in listening if we don’t do anything with that information we listened for. So we assign some instructions to execute when we successfully hear about that event occurring. myButton.addEventListener(“mouseClick”, celebrate); “Celebrate” is the custom name of a function that contains a set of instructions to execute. Since you specified the name, you are also responsible for creating that function with the instructions to execute.

  13. Event Model Syntax myButton.addEventListener(“mouseClick”, celebrate); function celebrate(e:MouseEvent) { trace(“Yay! Mouse was clicked”); } The e:MouseEvent is a mandatory MouseEvent datatype parameter. Every event listener sends an event data type to your function which is expected to accept it. That event contains a set of information that you can choose to look at and use. Most people don’t.

  14. Event Model Syntax myButton.addEventListener(“mouseClick”, celebrate); function celebrate(e:MouseEvent) { trace(e); } If you trace out the MouseEvent data type that was automatically accepted from the event listener, it will actually display a set of information that was sent to your function by the event listener. Using this information is optional.

  15. Removing listeners At some time in our lives, there may come a time when we no longer want to hear the nagging sounds of our spouse’s complains notifying us of every little event that occurs in the house. So we simply remove the listener from an object to stop listening to it. Note that we also have to specify what event state to stop listening for and the function that it should stop sending instructions to. myButton.removeEventListener(“mouseClick”, celebrate);

  16. Who broadcasts events? Button object MovieClip object Sound object Timer object Stage object Keyboard object Just to name a few. Endless possibilities really. Everyone broadcasts events. We’ll cover a few of those object types in future classes. Whenever something happens in Flash either by user action or some automated action, an event listener is the only way to know when something is happening so you can respond to it appropriately. Like secret spies, they are your awareness notifiers.

  17. Demonstrate Button Event Listener example in Flash

More Related