Let us take a small working sample of an application with events.
Create a flex project:
Here we are creating two canvases by using events we are going to communicate.
In main application we are adding listner
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
creationComplete=”init()” xmlns:local=”*”>
<mx:Script>
<![CDATA[
private function init():void {
this.addEventListener(MouseEvent.CLICK, clickEventHandler);
}
public function clickEventHandler(e:MouseEvent):void {
vs.selectedIndex = 1;
}
]]>
</mx:Script>
<mx:ViewStack id=”vs”>
<local:canvasOne />
<local:canvasTwo />
</mx:ViewStack>
</mx:Application>
First we have to add listner of an event for which we want to do some action on perticular event.
Here we added listner for the MouseClick event.
canvasOne.mxml:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml”
backgroundColor=”red” width=”400″ height=”300″>
<mx:Script>
<![CDATA[
public function clickEvent():void {
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
]]>
</mx:Script>
<mx:Label text=”Before dispatch event” />
<mx:Button label=”dispatch” click=”clickEvent()” x=”32″ y=”37″/>
</mx:Canvas>
Here we are dispatching an event, that will be handled in the main application.
The click handler in main application will have the logic to do further calculations.
Here we just changeing the index of the view stack.
canvasTwo.mxml:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml”
backgroundColor=”green” width=”400″ height=”300″>
<mx:Label text=”After dispatch event” />
</mx:Canvas>