Adding a second player

As Joshua mentioned in class, it would be good to add a second player in Space Invaders. To compensate for twice the players, we'll have to add more invaders as well.

Start by downloading the barebones Space Invaders from our class Games server.

We need some graphical objects for the game:

Design your ship to have a pointed end and a back end (or download one you find from the Internet). Set it up as a symbol named ship with the identifier alien_ship in properties (see the reminder of how added a marble to our Chinese Checker game if you need to).

Look at the stage and note the two player scoring objects. Take a look at their properties in the lower left when you select them so you get a sense of how a scoring area is created (or any text area for providing messages during a game).

Take a look at the ActionScript that sits behind our first Stage 1 frame:
The initialization function adds two new score variables and sets them to zero:

init();

function init(){
  globalDX = 5;
  globalDY = 0;

  row = 1;
  colOffset = 10;
  rowOffset = 60;
    
  playerScore = 0;
  oppScore = 0;
  
  setupAliens();
  setupShip();
} // end init
setupShip() is the routine called from our initiation function, init();

We need to add another ship in the setupShip() function (and rotate it to face downwards from the top):
function setupShip(){
	ship_ = [["1",275,10],["2",275,390]];
  
	//For loop to attach our container movieclip and pass the array information
	for (j=0; j<ship_.length; j++) {
		//attach the ship movie clip
		attachMovie("alien_ship", "ship"+j, j, {_x:ship_[j][1], _y:ship_[j][2]});
		//set a name property
		this["ship"+j].name.text = ship_[j][0];
	}
	theShip = eval("ship0");
	theShip._rotation = 180;
	
	theShip = eval("ship1");
	theShip.dy = 0;
	theShip = eval("ship0");
	theShip.dy = 0;	
and, we need to continue with a function as to what needs to happen each frame in the game with our ships:
	ship.onEnterFrame = function() {
		theShip = eval("ship1");
		theShip._y += theShip.dy;
		theShip = eval("ship0");
		theShip._y += theShip.dy;
		
		//left and right arrow key motion
		//ship only moves if an arrow key is down
		//ship only moves in x axis
	
		if (Key.isDown(Key.LEFT)){
		  theShip = eval("ship0");
		  theShip._x -= 5;
		} // end if
	
		if (Key.isDown(Key.SHIFT)){
		  theShip = eval("ship1");
		  theShip._x -= 5;
		} // end if
	
		if (Key.isDown(Key.RIGHT)){
		  theShip = eval("ship0");
		  theShip._x += 5;
		} // end if
		
		if (Key.isDown(Key.CONTROL)){
		  theShip = eval("ship1");
		  theShip._x += 5;
		} // end if
		
		if (Key.isDown(Key.TAB)){
		  theShip = eval("ship1");
		  theShip._y = 390;
		  theShip.dy = -10;
		} //end if
	
		if (Key.isDown(Key.SPACE)){
		  theShip = eval("ship0");
		  theShip._y = 10;
		  theShip.dy = 10;
		} // end if	
        
  	} // end enterFrame
} // end setupShip
We have added three new keys to control the second space ship on top - mirroring the bottom ship with the opposite values for y.

Our setupAliens() function is identical to the one player case except we need to check for hitting by the other player and increment the score for a player who successfully hits an alien(in green):
function setupAliens() {
	
  for (row = 1; row <= 5; row++){
    for (col = 1; col <= 10; col++){
    
      _root.attachMovie("alien", "alien_" + row + "_" + col, (row * 1000) + (col * 100));
      theAlien = eval("alien_" + row + "_" + col);
      theAlien._x = (col * (theAlien._width + 5) + colOffset);
      theAlien._y = (row * (theAlien._height + 5) + rowOffset);
      theAlien.onEnterFrame = function() {

        //move by global dx and dy values
        nextX = this._x + globalDX;
        nextY = this._y + globalDY;

        //check boundaries

        if (nextX > Stage.width){
          globalDX *= -1;
          //move down
          moveDown();
        } // end if
  
        if (nextX < 0){
          globalDX *= -1;
        } // end if
  
        this._x = nextX;
        this._y = nextY;
  
		theShip = eval("ship0");
        if (this.hitTest(theShip)){
          playerScore++;
          this.removeMovieClip(this);
          //reset ship
          theShip._y = 10;
          theShip.dy = 0;
        } // end if
        
        theShip = eval("ship1");
        if (this.hitTest(theShip)){
          oppScore++;
          this.removeMovieClip(this);
          //reset ship
          theShip._y = 390;
          theShip.dy = 0;
        } // end if
        
      } // end enterFrame
    } // end col loop
  } // end row loop
} // end setupAliens


It should look and work like this:



Save the completed project here by using the proper save option in the pop-up menu of your Web browser.