PDA

View Full Version : custom dispatchEvent loses my parameters - what am I doing wrong?


gpallis
07-14-2009, 02:01 AM
This is really confusing! The event sends and is received fine, but it gives an error trying to find the custom parameters.

Here's the event class:

package {

import flash.events.*

public class deadMouse extends Event{
public static const COMPLETION_TYPE:String = "Dead Mouse";
public var mouseData:Array;

public function deadMouse(theMouseData:Array) {
super(COMPLETION_TYPE);
mouseData = theMouseData;
}
}
}

Here's it being fired:
dispatchEvent(new deadMouse(["hi"]));

And here's it failing to be received:
function nextMouse(evt:Event) {
trace(evt.type) // this prints "Dead Mouse"
trace(evt.mouseData) // 1119 Access of possibly undefined property mouseData through a reference with static type flash.events:Event

Mazoonist
07-14-2009, 02:05 AM
Type the evt object to your custom event's type, not "Event":

evt:deadMouse

gpallis
07-14-2009, 02:06 AM
It works! You remain a saint, Mazoonist.

gpallis
07-14-2009, 02:08 AM
(and thanks again for all your help in my last dismal thread, trying to learn events at all - slow progress, clearly!)

Mazoonist
07-14-2009, 02:17 AM
The reason is that the Event class doesn't have a property called MouseData, but your more specific class that extends Event does.

I can't remember helping you in another thread, and your post count would seem to indicate that this is your only thread here. But if you say so... :)

You might try my website, I just recently added an article about dispatching events, although it's timeline based and maybe you are a step beyond that now, since you have extended the Event class. Still worth a read, tho. Also, this video tutorial of Lee Brimelow (Object Oriented Scrollbar) is very very good: http://www.gotoandlearn.com/play?id=71 (part 1)
http://www.gotoandlearn.com/play?id=72 (part 2)

Xegnma
07-14-2009, 02:55 AM
For the sake of polymorphism it may be better to keep the evt parameter typed as Event and then cast the event object back to the desired type inside the function body using the as keyword.

Mazoonist
07-14-2009, 03:03 AM
Xegnma,

That's a good point. Yes, you can also just cast the evt parameter to your custom type, too. Thanks!