error 1202, access to undefined property from a custom Event class
I'm using this code to create a custom Event class:
package evt {
import flash.events.EventDispatcher;
import flash.events.Event;
public class OtherEvent extends Event {
public static const OTHER:String = "OtherEvent";
public var data:*;
public function OtherEv开发者_StackOverflow中文版ent(type:String, data:*) {
this.data = data;
super(type, true);
}
}
}
This lets me easily pass Objects that I will find in the data property, so that if I pass:
var d = {point:50};
dispatchEvent(new OtherEvent(OtherEvent.OTHER,d));
I will find the value of the key 'point' by writing evt.data.point
.
The fact is that when I compile I receive a 1202 Error (Access to undefined property data in the package evt).
The strange thing is the following:
the error comes out only when I use the dot synthax: eg.
trace(evt.data.point);
I get no error when I write:
trace(evt['data']['point']);
Could you help me understand what it happens and why?
EDIT: in Strict Mode the first one blocks anything. When not in Strict Mode I get ReferenceError #1075 variable evt::data is not defined.
Make sure that your event handler is set to receive an instance of "OtherEvent" rather than "Event".
private function otherEventHandler(evt:OtherEvent):void {
trace(evt.data);
}
Also, rather than typing your "data" parameter with a wild-card, type it as an Object.
EDIT: Ah, I see now. I think your issue is that the class package is "evt" and the variable name in your event handler is also "evt". It's throwing an error to the compiler because it's trying to access a package called "evt.data". Simple fix - either change your class package to "events" (recommended) or change your variable name to something like "e".
Try to make your code as restrictive as possible:
package evt {
import flash.events.Event;
public class OtherEvent extends Event {
public static const OTHER:String = "OtherEvent";
public var data:Object; //specify the object type of your variable
public function OtherEvent(type:String, data:Object) {
super(type,true)
this.data = data; //create your super object first, then set data
}
}
}
To create and dispatch your object:
var d:Object = {point:50}; //Specify the object type of your variable
dispatchEvent(new OtherEvent(OtherEvent.OTHER,d));
To create an EventListener you will have to do something like
addEventListener(OtherEvent.OTHER,onOther);
And then finally your handler function
private function onOther(e:OtherEvent){
trace(e.data.point); //traces 50
}
This should do the trick for you
I see two items of note here
// change
var d = {point:50};
// to
var d:Object = new Object()
d.point = 50;
In AS3 you should always type cast your vars when you do not type cast you get some funny results as you are seeing.
And the other item to note is the "*" typing. Personally I never use this. It is error prone. Although, some others maybe disagree I would say avoid this at all costs.
So change
public var data:*;
//to
public var data:Object
// and you should get good results
If you make these two changes you should be ok.
精彩评论