<< Chapter < Page Chapter >> Page >

Produce the visual and audible effects of the lightening

The purpose of this method is to produce the visible and audible effects of the lightening other than the lightening bolt itself.

The method creates the flash from thelightening bolt, makes the tree more visible during the flash as shown in Figure 2, and plays a sizzle sound that will be followed by a clap of thunder.

Several steps are involved

The method begins by setting the alpha value on the tree image to 1.0 to cause the tree to become totally opaque.

Then it sets the value of the variable named sizzlePlaying to true to notify all other parts of the program that a sizzle sound isbeing played and a lightening bolt is being drawn.

Then it calls the play method on the sizzle Sound . The play method starts the sizzle sound playing and immediately returns a reference to a SoundChannel object through which the sound will be played.

The reference to the SoundChannel object is saved in the instance variable named channel .

Listing 13 registers an event listener on the SoundChannel object that will be executed when the sizzle sound finishes playing.

Finally, Listing 13 sets the red, green, and blue background color component values to dark yellow. These values along with the true value of sizzlePlaying will be used by the code in Listing 7 to set the background color of the canvas to dark yellow the next time the Timer object fires an event.

The drawLightening method

Listing 14 shows the method named drawLightening that is called by the CLICK event handler on the button in Listing 12 to draw the actual lightening bolt.

The method named drawlightening.

private function drawLightening():void {lighteningStartX = Math.floor(Math.random() * canvasObj.width / 3)+ canvasObj.width / 3; lighteningStartY =Math.random() * canvasObj.height / 10; lighteningEndX = canvasObj.width / 2 -6;lighteningEndY = canvasObj.height - treeBitMap.height + 10;//Draw a zero width dark yellow line to the starting// point of the lightening bolt. canvasObj.graphics.lineStyle(0, 0x999900);canvasObj.graphics.lineTo( lighteningStartX, lighteningStartY);//Set the line style to a bright yellow line that is// four pixels thick. canvasObj.graphics.lineStyle(4, 0xFFFF00);//Declare working variables.var tempX:uint; var tempY:uint = lighteningStartY;var cnt:uint;//Use a for loop to draw a lightening bolt with // twenty random segments.for (cnt = 0; cnt<20; cnt++ ) { //Compute the coordinates of the end of the next// line segment. tempX = Math.floor(Math.random()* canvasObj.width / 3) + canvasObj.width / 3;tempY = tempY + Math.floor(Math.random() * (lighteningEndY - tempY)/5);//Draw the line segment. canvasObj.graphics.lineTo(tempX,tempY);}//end for loop//Draw a line segment to the top of the tree. canvasObj.graphics.lineTo(lighteningEndX, lighteningEndY);//Make the lightening go to ground. canvasObj.graphics.lineTo(lighteningEndX, lighteningEndY + treeBitMap.height - 20);}//end drawLightening

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