Second-Player Functionality


The various angles in each player's tank, controls, and fire button made it impractical to frequently copy code from one player to another. And because it wasn't practical to make every change twice, I waited until this point to create the functionality for the second player. When and how to integrate two sets of code, and when to keep them separate, is a question whose answer is likely to change as your project progresses.

Player 2's Tank Movement

First I copied the code from the player1tank movie clip onto the player2tank movie clip. Then on the player2tank movie clip, I replaced all instances of player1target and player1controls with player2target and player2controls. I changed the if statement that determines which way to rotate the tank so that it evaluates to true if the result is less than 180 instead of greater than 180:

 if (_root.fixAngle(degrees-_root.fixAngle(this.body._rotation))<180) { 

I changed the if statement that determines whether the tank is within 5 ° of the destination angle to this:

 if (Math.abs(_parent.fixAngle(this.body._rotation+180)-degrees)<=5) { 

I changed the if statements that check the bounds of the angle indicator to these:

 if (degrees < 280 && degrees > 90) {     degrees=280;  } else if (degrees < 90) {     degrees=360;  } 

I changed the if statements that check the bounds of the direction indicator to these:

 if ((degrees > 90) && (degrees < 180)) {     degrees=180;  } else if ((degrees > 0) && (degrees <= 90)){     degrees=0;  } 

I changed the if statements that check the bounds of the power indicator to these:

 if ((degrees > 90) && (degrees < 180)) {     degrees=180;  } else if ((degrees > 0) && (degrees <= 90)){     degrees=0;  } 

Player 2's Tank Controls

Because the controls inside the controls symbol pointed to the right, I decided to reverse the controls for Player 2. I duplicated the controls symbol, naming it player2controls and renaming the controls symbol as player1controls.

On the player1tank symbol, I replaced all instances of "controls" with "player1controls," and on the player2tank symbol, I replaced all instances of "controls" with "player2controls."

On both tanks, inside the if statement that evaluated the dragging variable inside the angle control, I first changed the line that updates the turret based on the angle indicator:

 this.turret.gotoAndStop(Math.abs(Math.round((_root.fixAngle(degrees)- 270)/15))); 

Here, 270 replaced 90 because the control is pointing the opposite direction.

Also on both tanks, inside the if statement that evaluated the dragging variable inside the turret based on the direction indicator:

 this.turret._rotation = degrees+90; 

Again, the corrective angle is 180 ° different because it is pointing in the opposite direction.

Player 2's Fire Button

I could have added code to the fire buttons to disable them when it was the other player's turn , but because there was no way to keep the down state of the button from displaying, I decided to make them invisible. Because the _visible property has no effect on buttons, I had to embed the two fire button instances into separate movie clips. I named them player 1 fire and player 2 fire , and I gave them the instance names player1fire and player2fire. To hide Player 2's fire button when the game starts, I added the following code to frame 1 of the main timeline:

 // Hide player 2's fire button  player2fire._visible=false; 

To hide the fire button when it is pressed, I then added the following code inside the on(press) event on the fire button instance inside both the player1fire and player2fire movie clips:

 // Hide the fire button  this._visible=false; 

Because both fire buttons were now invisible during the bomb animation, I had to create a new currentplayer variable to keep track of which fire button should be made visible at the end of the animation. To do so, I added the following code to frame 1 of the main timeline:

 // Used to hide the fire buttons  currentplayer = 1; 

To toggle the currentplayer variable and make the appropriate fire button visible, I created a new switchPlayers() function by adding the following code to frame 1 on the main timeline:

 function switchPlayers() {     // Swap players      if (currentplayer==1) {         currentplayer = 2;      } else {         currentplayer = 1;      }      if (currentplayer==1) {         // Make the fire button visible          player1fire._visible=true;      } else {         // Make the fire button visible          player2fire._visible=true;      }  } 

To call this function at the end of the bomb animation, I added the following code to the last frame of the shell blast, sand blast, and water splash symbols:

 // Change players  _root.switchPlayers(); 

Player 2's Target

Just as I had done with the player1target movie clip, I created a new Player 2 target movie clip and drew a shape in it that was slightly smaller than Player 2's island. I gave it an instance name of player2target.

Because there was already an onClipEvent(enterFrame) event on the player1target instance, I decided to add code on the player1target movie clip to also swap the mouse icon when the mouse was over the new player2target instance:

 if ((_root.player1target.hitTest(_root._xmouse, _root._ymouse, true))   (_root.player2target.hitTest(_root._xmouse, _root._ymouse, true))) { 


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