<< Chapter < Page Chapter >> Page >

The last statement in the constructor in Listing 4 registers a CREATION_COMPLETE event handler on the Canvas object. You are already familiar with event listeners of this type. The code for thelistener is shown in its entirety in Listing 5.

A creation_complete event handler.

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 smallSky // image.this.width = Bitmap(smallSky.content).width; this.height = Bitmap(smallSky.content).height;//Add the image to this Canvas object.this.addChild(smallSky);//Register a timer listener and start the timer // running.timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start();} //end creationCompleteHandler

As you learned in earlier lessons, this handler method is executed when the Canvas object has been fully created.

Nothing new here

There is nothing new in Listing 5. The code in Listing 5:

  • Sets the width and the height of the Canvas object to match the width and the height of the image that is displayed while the program is running.
  • Adds the image to the Canvas object.
  • Registers an event listener on the Timer object that was instantiated in Listing 3 and starts the timer running to fire tenevents per second.

Beginning of the TIMER event handler

The event handler that is registered on the Timer object begins in Listing 6. This method is executed each time the Timer object fires an event.

Beginning of the timer event handler.

public function onTimer(event:TimerEvent):void {//Update the loop counter. loopCntr++;if (loopCntr == int.MAX_VALUE - 1) { //Guard against numeric overflow.loopCntr = 0; }//end if

Update the loop counter

Among other things, the code in the Timer event handler maintains a count of the number of events that have been fired by the timer. Thecode in Listing 6 increments the timer each time the event-handler method is executed, and sets the value back to zero when it reaches a very large value toguard against binary overflow.

Play an occasional wind sound

According to the code in Listing 3, the Timer object will fire an event every 100 milliseconds, or ten times per second. That causes theevent handler to be called ten times per second.

The code in Listing 7 uses the modulus operator to identify every 25th call to the event handler. This occurs approximately once every 2.5 seconds,depending on the accuracy of the timer.

Play an occasional wind sound.

if ((loopCntr % 25 == 0)&&(Math.random()>0.75)){ wind.play();}//end if

Let the wind blow: or maybe not

When the code in Listing 7 determines that 2.5 seconds have passed since the last attempt to play the wind sound, it gets a random value of type Number with a value between 0 and 1.0. If that random value is greater than 0.75, it calls the play method on the wind sound to cause the sound to be played once from start to finish.

One wind sound every ten seconds on average

Assuming that the random values are uniformly distributed, about one out of every four random values will be greater than 0.75. Therefore, the wind soundshould be played about once every ten seconds on average.

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