<< Chapter < Page Chapter >> Page >

Approximately three Timer events per second

As you are aware, the event handler that begins in Listing 5 is executed each time the Timer object fires an event. The Timer object was instantiated in Listing 1 and configured to fire an event every 35milliseconds, or approximately three times per second.

The only thing that is new in the fragment shown in Listing 5 is the pair of calls to the methods named:

  • processBackgroundColor, and
  • makeTheCloudsMove

The purpose of each of these methods is described by its name. I will explain both methods later in this lesson.

Controlling the motion of the moon

The remainder of the Timer event handler, which is shown in Listing 6, is dedicated to causing the moon to move very slowly from left toright across the screen as shown in Figure 1 and Figure 2.

Controlling the motion of the moon.

//Draw a filled circle on this Canvas object. if (!sizzlePlaying) {//Erase the circle. Note that this would also // erase the lightening bolt if it were done while// the sizzle sound is playing. graphics.clear();}//end if//Make the circle move a very small distance to the // right. Make it wrap and reappear on the left//when it reaches the right side of the window. circleX += dx;if (circleX>canvasObj.width - radius) { circleX = 5 * radius;}//end if graphics.beginFill(0xffff00);graphics.drawCircle(circleX,circleY,radius); graphics.endFill();}//end onTimer

The code in Listing 6 draws a yellow filled circle a little further to the right each time the Timer fires an event. When the circle reaches the right edge of the Flash window, it starts over again on the left.

Erase the old moon before drawing the new moon

It is necessary to erase the old circle before drawing each new filled circle. Otherwise, instead of seeing a filled circle moving from left to right, theviewer would see a very wide yellow line being slowly drawn across the screen.

Houston, we have a problem

The proper way to erase the old filled circle is to call the clear method of the Graphics class. However, this is also the proper way to erase the yellow lightening bolt that I willexplain later. Therefore, it is critical that the clear method not be called while the lightening bolt is on the screen.

The Boolean variable named sizzlePlaying

The Boolean variable named sizzlePlaying is used to control several aspects of the program relative to the periodduring which the sizzle sound is played, the lightening bolt is drawn, and the scene is illuminated by the lightening bolt.

The value of this variable is set to false when the variable is declared in Listing 1. It is set to true when the sizzle sound begins playing and is setback to false when the sizzle sound finishes playing. Thus, it is always true while the sizzlesound is playing and is false at all other times.

An egg-shaped moon

The value of sizzlePlaying is used in Listing 6 to prevent the clear method from being called while the lightening bolt is on the screen. This actually causes the moon to take on a slight egg shapeduring that period because new versions of the moon are being drawn without erasing the old versions. However, this isn't visually apparent because the moonmoves such a short distance during that period. Also, the lightening bolt and not the moon probably commands the attention of the viewer during this period sothe distortion isn't very noticeable.

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