-
Making custom event
I'm trying to make a custom event that can contain some info like a string or an array. But it won't work... In the code below I have rectangle and a textfield on stage. The rectangle listens for a mouse click event, and the textfield listens for a customevent. When the rectangle is clicked a function dispatches a customevent, and a message should be written in the textfield. But it doesn't happen. Why?
Code:
import lib.CustomMouseEvent;
// rectangle on stage
rectangle.addEventListener(MouseEvent.CLICK, onClick);
// textfield on stage
tf.addEventListener(CustomMouseEvent.mouseType, write);
function onClick(evt:MouseEvent){
var cust:CustomMouseEvent = new CustomMouseEvent(CustomMouseEvent.mouseType);
cust.setMsg("Hello!!!");
dispatchEvent(cust)
}
function write(evt:CustomMouseEvent){
tf.text = evt.getMsg();
}
public class CustomMouseEvent extends Event{
public static const mouseType:String = "selection";
var msg:String;
public function CustomMouseEvent(type:String){
super(type, true);
}
public function setMsg(m:String){
msg = m;
}
public function getMsg():String{
return msg;
}
}
Last edited by vator; 09-17-2009 at 04:31 AM.
-
half as fun, double the price
you have to use addEventListener on the object dispatching the event. Just like you added the CLICK listener to the rectangle (since the rectangle dispatches the CLICK event), you would need to add the listener for CustomMouseEvent.mouseType to the object dispatching that event. Given your code, it seems like that would be the object defining the onClick which is ... the main timeline? If so, then you would add the listener to this, not tf - that is unless you dispatched the event from tf.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|