
Associate the listener with an event with ActionScript 149
4. Enter the following function immediately before the convertCurrency function in the
<mx:Script> tag:
public function createListener():void {
btnConvert.addEventListener(MouseEvent.CLICK, convertCurrency);
}
When the user clicks the btnConvert button, the convertCurrency event listener is
notified that a triggering event has occurred. The listener function performs the currency
calculation and displays the results.
The script block should look as follows:
<mx:Script>
<![CDATA[
import flash.events.MouseEvent;
public function createListener():void {
btnConvert.addEventListener(MouseEvent.CLICK, convertCurrency);
}
public function convertCurrency(e:MouseEvent):void {
var rate:Number = 120;
var price:Number = Number(txtPrice.text);
if (isNaN(price)) {
lblResults.text = "Please enter a valid price.";
} else {
price = price * rate;
lblResults.text = "Price in Yen: " + String(price);
}
}
]]>
</mx:Script>
5.
In the <mx:Application> tag, enter the following property so that the
createListener() function is called and the event listener is registered immediately after
the application is created:
creationComplete="createListener();"
Kommentare zu diesen Handbüchern