R – Dispatch an event from a class that inherits from EventDispatcher

actionscript-3apache-flexflash

I've got a class that extends EventDispatcher.

What I want to do is to dispatch the click event when the component is clicked. (The class is essentially some text in a textfield that needs to be able to do certain things, and it needs to be able to respond to a click). Sounds easy enough… I want the event dispatched when that portion of the text is clicked. But uh…how? it's not like a button where I can just go

myButton.addEventListener(MouseEvent.CLICK, myClickHandler);

That's clear, because some component is going to be listening for the Click event dispatched when myButton is clicked. It is built into the AS3 framework that a button knows how to listen for a click event.

After the import statements I've got:

[Event(name="click" type="mx.events.Event")]

How do I dispatch the event when the component is clicked, when the component doesn't yet know how to respond to a click event? I've tried adding an event listener in the textfield which contains this custom class of text, but nothing's happening because the Click event hasn't been dispatched.

Best Answer

You can create your own click event and dispatch it. You can do that also to dispatch clicks on objects where no user ever have clicked :D

Try this:

var mEvent:MouseEvent = new MouseEvent(MouseEvent.CLICK, [HERE MORE PARAMS BY YOU]);
yourObject.dispatchEvent(mEvent);

Now, you will recieve Click Events from yourObject.

Related Topic