Its not necessary to handle the events inside the tags only, you can even handle them manually as well as follows:
<?xml version=”1.0″?>
<!– events/DispatchEventExample.mxml –>
<mx:Application xmlns:mx=” http://www.adobe.com/2006/mxml” initialize=”createListener(event)”>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function createListener(e:Event):void {
// b1.addEventListener(MouseEvent.MOUSE_OVER, myEventHandler);
b1.addEventListener(MouseEvent.CLICK, myClickHandler);
}
private function myEventHandler(e:Event):void {
var result:Boolean = b1.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
}
private function myClickHandler(e:Event):void {
Alert.show(”Triggered by the ” + e.type + ” event”);
}
]]>
</mx:Script>
<mx:Button id=”b1″ label=”Click Me”/>
</mx:Application>
check out one more example:
<?xml version=”1.0″?>
<!– events/DispatchCustomizedEvent.mxml –>
<mx:Application xmlns:mx=” http://www.adobe.com/2006/mxml” creationComplete=”addListeners()”>
<mx:Script>
<![CDATA[
private function customLogEvent(e:MouseEvent):void {
ta1.text = e.currentTarget.id + “:” + e.type + “:” + e.shiftKey;
// Remove current listener to avoid recursion.
e.currentTarget.removeEventListener(”doubleClick”,customLogEvent);
}
private function handleEvent(e:MouseEvent):void {
// Add new handler for custom event about to be dispatched.
e.currentTarget.addEventListener(”doubleClick”,customLogEvent);
// Create new event object.
var mev:MouseEvent = new MouseEvent(”doubleClick”);
// Customize event object.
mev.shiftKey = true;
// Dispatch custom event.
e.currentTarget.dispatchEvent(mev);
}
private function addListeners():void {
b1.addEventListener(”click”,handleEvent);
b2.addEventListener(”click”,handleEvent);
}
]]>
</mx:Script>
<mx:VBox id=”vb1″>
<mx:Button id=”b1″ label=”B1″/>
<mx:Button id=”b2″ label=”B2″/>
<mx:TextArea id=”ta1″/>
</mx:VBox>
</mx:Application>
|