The Bomb


I created a new bomb movie clip, which consisted of a simple circle shape, and put it inside the tank symbol. Later I created a fire button, which attached the bomb movie clip and sent the variables .

Note

Although variables default to 0 in Flash, it's a general programming practice to initialize your variables.


To get the bomb ready to act on the variables sent by the fire button, I placed the following code onto the bomb movie clip:

 // Initialize the height variable  onClipEvent(load) {     z = 0;  }  onClipEvent (enterFrame) {     // Is the bomb is still above ground?      if (z>=0) {         // Position the bomb according to the variables sent          this._x = x;          this._y = y;          // Display the z (height) by changing the scale          this._xscale = 100+(z*10);          this._yscale = 100+(z*10);          // Change x and y speeds based to account for wind          xSpeed += (xWind/20);          ySpeed -= (yWind/20);          // Change z speed to effect force of gravity          zSpeed -= 2;          // Update three dimensions based on changes in speed          x += xSpeed;          y -= ySpeed;          z += zSpeed;      } else {         // The bomb is below ground, do not display.          // Reset the bomb to 100% scale          this._xscale=100;          this._yscale=100;      }  } 

The z variable, which controls the height of the bomb, is initialized by the onClipEvent(load) event as soon as the bomb is attached. The if statement checks the z variable to see whether it is greater than 0, indicating that the bomb is still above the ground. If it is greater than 0, the _x and _y properties of the bomb are first set to the values of the x and y variables, sent by the fire button. Then the height is displayed by adjusting the scale of the bomb. The _xscale and _yscale properties of the bomb are set by multiplying the value of z (the height) by an arbitrary value of 10 and then adding 100. The reason 100 is added is so that the scale will be 100% when the bomb is at ground level. The zSpeed is decreased by 2 to simulate gravity. A series of if statements use the value of zSpeed to determine which frame of the bomb animation to display. As the zSpeed value decreases, the playhead in the bomb animation is slowly advanced. When the value of z becomes negative, the bomb is below the ground, so the _xscale and _yscale properties of the bomb are reset to 100%.

Note

The numbers are arbitrary because the _xscale and _yscale properties are expressed as a percentage of full size , the control indicators use the _rotation property in each control, and the height (z) and vertical speed (zSpeed) are based on an arbitrary scale where z=1 means 100%. So, they really have nothing to do with each other except that they're in the same game.


Fire Button

To disable the fire button when the tank is rotating or moving, I put everything inside the on(press) event on Player 1's fire button inside the following:

 if (!_root.player1tank.rotation && !_root.player1tank.motion) { 

To tell the bomb how the controls were set, I placed the following code on the fire button for Player 1:

 on (release) {     // Calculate control values      angle=90-Math.abs(_root.player1controls.angle.indicator._rotation);      direction=_root.player1controls.direction.indicator._rotation-90;      power=(((180-_root.player1controls.power.indicator._      rotation)/1.8)+80)/4;  } 

So, when the fire button is pressed, the movie clip containing it is hidden, which also hides the fire button. The new angle variable is set by subtracting the absolute value of the _rotation property of the angle control's indicator movie clip from 90 because the _rotation property decreases as the indicator value increases . The new direction variable is set to 90 less than the _rotation property of the direction control's indicator movie clip. To calculate the new power variable, the value of the _rotation property of the power control's indicator movie clip is first subtracted from 180. This is because the value of the _rotation property decreases as the property control is turned up. Then this number is divided by 1.8 to shift it into the range of 0 “100. This number is added to 80, which was arbitrarily selected to make sure that the blast has some power even if the control is turned all the way down. All of this is then divided arbitrarily by the number 4 to lower the value. Both of these arbitrary numbers were selected based on trial and error.

To calculate the initial speeds on the X-, Y-, and Z-axes, I added the following code to the fire button:

 // Calculate percentage of force that is upward vs. lateral  zSpeed = angle/90;  xandySpeeds = 1-zSpeed;  // Multiply upward & lateral forces by power  zSpeed *= power;  xandySpeeds *= power;  // Distill lateral force into x and y  radians = (Math.PI/180)*direction;  xSpeed = Math.cos(radians);  ySpeed = -Math.sin(radians);  // Calculate the actual x and y Speeds  xSpeed *= xandySpeeds;  ySpeed *= xandySpeeds; 

Because there are 90 ° degrees between the horizontal and vertical axes, dividing the angle control's vale by 90 reveals the percentage of upward force. To distill the upward force from the lateral force, I divided the angle variable by 90 and assigned it to the new variable zSpeed. I subtracted this number from 1 to determine what percentage was lateral, and I assigned it to a new variable, xandySpeeds. Then I multiplied both zSpeed and xandySpeeds by the power variable to take the power control's setting into account.

Distilling the xandySpeeds into new xSpeed and ySpeed variables requires creating a new variable called radians and giving it the value of the direction value in radians. Flash 5's built-in Math.sin() and Math.cos() functions convert the radians variable into xSpeed and ySpeed. The ySpeed variable is negative because, in Flash, the Y-coordinates increase in value as you move down the screen.

To calculate the wind variables, I added the following code to the fire button:

 // Calculate x and y speeds of the wind  radians = (Math.PI/180)*_root.wind.direction._rotation;  xWind = Math.sin(radians)*_root.wind.speed.indicator._xscale/10;  yWind = -(Math.cos(radians)*_root.wind.speed.indicator._xscale/10); 

As with the xSpeed and ySpeed variables, the built-in Math.sin() and Math.cos() functions calculate the relative X and Y speeds of the wind. To take the wind's speed into account, the resulting values are multiplied by one tenth of the _xscale property of the wind speed indicator. Again, the yWind variable is negative because numbers increase as you go down the screen.

To attach the bomb movie clip and send the appropriate variables to it, I added the following code to the fire button:

 // Attach the bomb controller  _root.attachMovie("bomb", "thebomb", 10);  // Rotate the bomb to match the turret direction  _root.thebomb._rotation=_root.player1controls.direction.  indicator._rotation-90;  _root.thebomb.x = _root.player1tank.turret._x;  _root.thebomb.y = _root.player1tank.turret._y;  _root.thebomb.xSpeed = xSpeed;  _root.thebomb.ySpeed = ySpeed;  _root.thebomb.zSpeed = zSpeed;  _root.thebomb.xWind = xWind;  _root.thebomb.yWind = yWind; 

First the bomb movie clip is rotated so that it matches the turret's rotation. The initial x and y variables of the attached bomb movie clip are set to the _x and _y properties of Player 1's turret. The initial xSpeed, ySpeed, and zSpeed variables are sent, as are the constant xWind and yWind variables.



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