Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;

Similar presentations


Presentation on theme: "Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;"— Presentation transcript:

1

2

3 Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;

4 Functions function myFunctionName() { //instructions go here }

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

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

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

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

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

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

11 Events are different actions an object can be doing.

12

13 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;

14 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”);

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

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

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

18 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);

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

20 Demonstrate Button Event Listener example in Flash


Download ppt "Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;"

Similar presentations


Ads by Google