Wind


To make the game a little more unpredictable, I created a wind movie clip that dynamically generates a random speed and direction for the wind factor. The factor affects the direction and speed of the bomb. To randomly generate the wind's speed and direction when the movie starts, as well as at the end of each game, I placed the new randomizeWind() function in frame 1 on the main timeline:

 // Randomizes wind speed and direction  function randomizeWind() {     wind.windspeed = Math.round(Math.random()*100);      wind.winddirection = Math.round(Math.random()*360);      // Activate the change in wind speed and direction      // in the MC's onClipEvent(enterFrame)      wind.changedir = true;      wind.changespeed = true;  } 

All four variables are set inside the wind movie clip. Although the built-in random() function would have returned a pseudorandom integer, it has been deprecated in Flash 5. It's a good idea to use the new Math.random() function, which generates a pseudorandom number between 0 and 1. To set the windspeed variable in the wind movie clip, I first multiplied a random number by 100 to shift it into the 0 “100 range. If the range needed to start with a number other than 0, I would have had to add that number as well. Then I used the built-in Math.round() function to round this number to the nearest integer so that it could be compared inside the wind symbol. To set the winddirection variable, I multiplied a random number by 360 to shift it into the 0 “360 range, and I then rounded this number to the nearest integer. Finally, I set the changedir and changespeed variables to true to activate the wind and speed animations inside the wind movie clip.

To cause the wind speed and direction to be randomized when the first game starts, I placed the following code in frame 1 on the main timeline:

 // Randomize wind when the movie first starts  randomizeWind(); 

The code to cause the wind speed and direction to be randomized at the beginning of a new game would be added once support for multiple games was added.

Animating the Wind Changes

Although a linear animation can be created using a motion tween, a dynamic animation requires the use of an onClipEvent(enterFrame) event that occurs each time the frame plays.

To animate the change in wind speed, I placed the following code on the wind symbol:

 onClipEvent (enterFrame) {     // If the randomizeWind() function on the main timeline has      // activated the change in speed      if (changespeed) {         // Calculate the total amount the speed needs to change          speeddiff = speed.indicator._xscale-windspeed;          // If the speed is done changing, turn off the activation          variable          if (speeddiff == 0) {             changespeed = false;          // If the speed needs to increase          } else if (speeddiff>0) {             // If the speed change is less than 10%, change slowly              if (speeddiff<10) {                 speed.indicator._xscale -= 1;              // Otherwise change quickly              } else {                 speed.indicator._xscale -= 10;              }          // If the speed needs to decrease          } else if (speeddiff<0) {             // If the speed change is less than 10%, change slowly              if (speeddiff>-10) {                 speed.indicator._xscale += 1;              // Otherwise change quickly              } else {                 speed.indicator._xscale += 10;              }          }      }  } 

The first if statement checks the value of changespeed. When the randomizeWind() function on the main timeline sets this value to true, the wind speed is changed a little bit at a time. First, a new speeddiff variable is set to the difference between the _xscale of the speed indicator movie clip and the windspeed variable. If the numbers are equal, the changespeed variable is set to false so that this code stops executing. If the difference is positive, numbers are added, and vice versa. If the difference is more than 10% of the _xscale, it is changed by that amount. Otherwise, it is changed by 1%.

To animate the change in wind direction, I added the following at the end of the code inside the onClipEvent(enterFrame) on the wind symbol:

 if (changedir) {     dirdiff = _root.fixAngle(direction._rotation-winddirection);      // If the direction is done changing      if (dirdiff == 0) {         changedir = false;      // If the direction needs to rotate to the left      } else if (dirdiff<180) {         // If the change in direction is less than 10 degrees,          // change slowly          if (dirdiff<10) {             direction._rotation -= 1;          // Otherwise change quickly          } else {             direction._rotation -= 10;          }      // If the direction needs to rotate to the right      } else if (dirdiff>180) {         // If the change in direction is less than 10 degrees, change          slowly          if (dirdiff>350) {             direction._rotation += 1;          // Otherwise change quickly          } else {             direction._rotation += 10;          }      }  } 

The first if statement checks the value of changedir, which is set by the randomizeWind() function on the main timeline. If the value is true, the wind direction is changed a little bit at a time, whether the wind speed is changing simultaneously or not.

To set the value of the dirdiff variable, the _rotation property of the direction movie clip is first compared to the winddirection variable set by the randomizeWind() function. This value is sent to the fixAngle() function to shift it to the range of 0 “360. If the numbers are equal, the changedir variable is set to false so that this code stops executing. If the difference is positive, numbers are added, and vice versa. If the difference is more than 10 °, it is changed that amount. Otherwise, it is changed by 1 °.

In the upcoming section "The Bomb," the fire button would assign values to two new variables to tell the bomb how much the wind should affect the bomb's movement.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net