If you’re writing a subclass of EventDispatcher, you can compile it if it contains code like this:
private var oneEvent:Event = new Event("whatever.");
public function dispatchOneEvent():void {
dispatchEvent(oneEvent);
}
That same class can listen for that event, and respond to it multiple times. This is nice; it suggests that an already dispatched event object can be “reused” by its dispatcher. But all that falls apart when the listener is another object. You get an error like this:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@222f241 to net.rezmason.ExamplePackage.CustomEvent.
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at net.rezmason.ExamplePackage::ExampleDispatcher/update()
at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
at flash.utils::Timer/flash.utils:Timer::tick()
Notice that this particular error involves a subclass of Event, rather than an Event object itself. I haven’t tested to see whether this happens with plain Event objects, but if I could have used simple events I would have. And I can’t, so I didn’t.
What I want to know is, what is the exact reason for this? What happens to events in this case that prevents them from being reused? Because events are inarguably the most common type of garbage in Flash, and I think it’d be just grand if I didn’t to dispatch new ones every time.