Josh Buhler - addEventListerner() - Now What?
-
Upload
360conferences -
Category
Technology
-
view
716 -
download
1
description
Transcript of Josh Buhler - addEventListerner() - Now What?
Josh Buhler
addEventListener() - Now what?
I like long walks on the beach...
WHO AM I?
CURRENTLY Senior Developer at Rain. Building Flash, Flex, iPhone apps, and bringing the hurt in CoD4.
PREVIOUSLY Flash Ninja at Footnote.com, Flash developer at mediaRAIN. (Yes, same place I’m at now, just a different name. Long story.) Basically, I’ve been writing ActionScript in some form since 2003.
CONFERENCES Adobe MAX, 360|Flex San Jose, Flash Forward
If you decide to walk out now, it’s OK. Really.
TODAY
WHAT IT IS Events 201. We’ll discuss the basics just a little bit, but then get into more intermediate topics and practical examples.
WHAT IT’S NOT The molecular structure of Events, how to hack the byte-code of the .SWF to mod your events, or super-bizarre edge cases.
Trust me. There’s actually a plan.
THE AGENDA
•What an Event is
•How they work
•How to use them
•Dispatching events
•Removing event listeners
Really. I promise.
THE AGENDA
•Creating custom events
•Event Priority
•The Event Flow
•Event targets
•Default Behaviors
•Killing events
WHAT IS
AN EVENT?
Noteworthy... yeah. I was gonna say that.
WHAT IS AN EVENT?
”A noteworthy runtime occurrence that has the potential to trigger a response
in the program.”
TYPES OF EVENTS
BUILT-IN events are defined and dispatched by the Flash Player
CUSTOM events are defined and dispatched by the developer’s code.
The Freebies
BUILT-IN EVENTS
MouseEvent.CLICK
KeyboardEvent.KEY_DOWN
Stage
Loading
Uploading
If you build it, they will come... or something like that.
CUSTOM EVENTS
XML Parsing Complete
Game Over
Application State Changes
Custom Components
You’re old enough, it’s time we had the talk.
EVENT BASICS
SOMETHING HAPPENS This is one of those noteworthy runtime occurrences.
CREATION An instance of Event, or a subclass of it is created.
DISPATCH The created Event instance is dispatched to anything that cares.
HANDLING Something receives the event dispatch. Usually.
EVENT COMPONENTS
EVENT DISPATCHERS
EVENT OBJECTS
EVENT HANDLERS
EVENT OBJECTS
TYPE Either an instance of Event, or a subclass of it.
NAME The name of the Event. (Kinda obvious, right?)
PHASE Where in the event flow this Event is at.
TARGET What triggered this Event, and what’s handling it.
USING EVENTS
NAME of the Event
TYPE of Event
REGISTER to listen for the Event
HANDLE the Event when it happens
WAIT for the Event to occur.
EVENT NAMES
EVENT TYPES
REGISTER FOR THE EVENT
REGISTER FOR THE EVENT
addEventListener (type:String, listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false);
REGISTER FOR THE EVENT
addEventListener (type:String, listener:Function);
DISPATCHING EVENTS
CREATE an instance of the Event to be dispatched
DISPATCH the Event
REMOVING EVENT LISTENERS
UNNECESSARY Sometimes you no longer care about that Event.
MEMORY Use less of it.
CPU Reduce the load.
Quick, easy...
ANONYMOUS HANDLERS
...and very, very bad.
ANONYMOUS HANDLERS
This picture is one.
WEAK REFERENCES
EventDispatcher
EventDispatcher
addEventListener()
Listener
EventDispatcher
addEventListener()
Listener
EventDispatcher
WEAK REFERENCES
WEAK REFERENCES
PREVENTS listeners from becoming stranded
WEAK REFERENCES
PREVENTS listeners from becoming stranded
NO GUARANTEES when they’ll be cleaned up
FORCE GARBAGE COLLECTION Unsupported, and SHOULD NOT be used in production code
http://bit.ly/IU0Z5
Unless you still need them, of course
ALWAYS REMOVE EVENT LISTENERS
It’s alive!
CUSTOM EVENTS
NAME-ONLY Still basic events, only their name has changed.
EXTEND When your Events need to carry additional properties, or provide other extra functionality.
EXTENDING EVENT
OVERRIDE clone() and toString()
EXTEND When your Events need to carry additional properties, or provide other extra functionality.
MORE INFO http://bit.ly/Fwb64
EVENT METADATA
[Event(name="tempChange", type="examples.TemperatureEvent")]
EVENT PRIORITY
EVENTS registered for the same event, with the same object fire in the order they were registered.
UNLESS you change their priority when registering.
HIGHER priority listeners will be triggered first.
THE
EVENT
FLOW
Stage
Sprite
TextField
Bubble Phase
Capture Phase
Target Phase
Think of the possibilities...
EVENT FLOW
Centralize, yo.
EVENT FLOW
Stay on target...
EVENT TARGETS
DEFAULT
BEHAVIOR
KILLING EVENTS
KILLING EVENTS
stopPropagation() prevents the Event from continuing through the remainder of the event flow.
stopImmediatePropagation() stops the Event in it’s tracks. Do not pass Go. Do not collect $200.
Worst. Session. Ever.
THE END
Have your people call my people.
CONTACT
ghostRadiojoshbuhler
ghostRadio.netmediaRain.com
SAMPLE CODE http://www.ghostradio.net/events360