views:

116

answers:

4

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.

A: 

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.

Paratron
A: 
bhups
Thnks, but the class isn't the textfield -- it is contained in the textfield. I did, though, have the listener and handler in the textfield itself (which I think is what you're saying and which I think is correct). It just doesn't work.
David
Can you provide some piece of code from your class? (may be skeleton)
bhups
A: 

OK, I tried this in the constructor:

    var mEvent:MouseEvent = new MouseEvent(MouseEvent.CLICK, true, false);
this.dispatchEvent(mEvent);

Then, in the containing textfield, while iterating through these objects (each of which is called cp), I did this:

                cp.addEventListener(MouseEvent.CLICK, mouseClickHandler);

Finally the mouseClickHandler:

 private function mouseClickHandler(event:MouseEvent):void 
 {
  trace("Clicked!!!!!!!!!!!");
 }

Running in debug mode I get nada. Nunca. Niente. Nuttin'. Which is to say: no trace of being clicked. Did I do something wrong?

David
You generally shouldn't dispatch events in constructors (they can't be listened to) -- they are only addressable *AFTER* the constructor is completed.
Christopher W. Allen-Poole
A: 

If I understand you correctly, you can't. Until addEventListener is called on an object, there is no means whereby you can listen to the event, but please provide more explicit code, because I don't think anyone has really approached answering your question.

Christopher W. Allen-Poole