<< Chapter < Page Chapter >> Page >

The CREATION_COMPLETE event handler

The code in the constructor in Listing 3 registers a CREATION_COMPLETE event handler on the Canvas object. You are already familiar with the use of event handlers of this type.

The CREATION_COMPLETE event handler is shown in its entirety in Listing 4. As before, there is nothing in Listing 4 that I haven't explained in previouslessons, so no explanation beyond the embedded comments should be needed.

The creation_complete event handler.

//This handler method is executed when the Canvas has // been fully created.private function creationCompleteHandler( event:mx.events.FlexEvent):void{//Set the width and height of the Canvas object// based on the size of the bitmap in the // normalSky image.this.width = Bitmap(normalSky.content).width; this.height = Bitmap(normalSky.content).height;//Add the images to the Canvas object. Note that// the two images are overlaid at 0,0. this.addChild(normalSky);this.addChild(flippedSky);//Add a button in the upper-left corner in front // of the sky images and register a CLICK event// handler on the button. button = new Button();button.x = 10; button.y = 10;button.addEventListener(MouseEvent.CLICK, onClick); button.label = "Click Me";button.setStyle("color", 0xFFFF00); addChild(button);//Get and save a reference to a Bitmap object // containing the contents of the tree file.treeBitMap = Bitmap(tree.content);//Place the treeBitMap in a new Image object and // place it on the canvas near the bottom center of// the canvas. treeBitMap.x =canvasObj.width / 2 - treeBitMap.width/2;treeBitMap.y = canvasObj.height - treeBitMap.height; newTreeImage.addChild(treeBitMap);this.addChild(newTreeImage);//Make the tree almost invisible. It will be made // highly visible in conjunction with a// lightening flash. newTreeImage.alpha = 0.2;//Cause the blue background of the tree to// be transparent. processChromaKey(treeBitMap);//Register a timer listener and start the timer// running. timer.addEventListener(TimerEvent.TIMER, onTimer);timer.start(); } //end creationCompleteHandler

Beginning of the TIMER event handler

The last two statements in Listing 4 register a TIMER event listener on the Timer object that was instantiated in Listing 1 and start the timer running.

The TIMER event handler begins in Listing 5. There is some interesting new code in this method, so I will break it down and explain it infragments.

Beginning of the timer event handler.

//TimerEvent handler. This method is executed each // time the timer object fires an event.public function onTimer(event:TimerEvent):void {//Update the loop counter. Several things depend on // this counter.loopCntr++; if (loopCntr>int.MAX_VALUE-2) { //Guard against numeric overflow.loopCntr = 0; }//end if//Play a wind sound every 100th timer event only// if a random value is greater than 0.5. This // should happen half the time on the average.if ((loopCntr % 100 == 0)&&(Math.random()>0.5)) { wind.play();}//end if//Make random changes to the background color. processBackgroundColor();//Make changes to the alpha values of the normal// and flipped sky images. makeTheCloudsMove();

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask