Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - A Scrolling playfield Current Page - 26 - Monster respawning   Next Page - Simple sound
  Return to the index of free Java tutorials  

Monster respawning

(c) Alexander Hristov

Currently when we kill a monster nothing special happens, and the monsters are quickly exterminated. We can take several approaches here:

  • Spawn new monsters as older ones are killed
  • Change the current "level" switching to different backgrounds and different critters
  • Making new monsters appear periodically regardless of how many are killed

To illustrate our point, we'll use the first approach here - we'll spawn a new monster for each monster killed. This is as easy as adding the following to our Monster class:


           . . .  
33      public void collision(Actor a) {
34        if (a instanceof Bullet || a instanceof Bomb) {
35          remove();
36 spawn();
37 stage.getPlayer().addScore(20); 38 } 39 } 40
41 public void spawn() { 42 Monster m = new Monster(stage); 43 m.setX( (int)(Math.random()*Stage.WIDTH) ); 44 m.setY( (int)(Math.random()*Stage.PLAY_HEIGHT/2) ); 45 m.setVx( (int)(Math.random()*20-10) ); 46 stage.addActor(m); 47 }
48 49 public void fire() { . . .


Do you want to be notified when new tutorials or lessons are published? Press here


Full list of Java source files for this step

Bomb.java Bullet.java Laser.java Stage.java
Monster.java SpriteCache.java Invaders.java Actor.java
Player.java      

Full list of resources

explosion.wav disparo2.gif bombUL.gif oceano.gif
bombDR.gif disparo0.gif bicho0.gif bombD.gif
missile.wav test.gif disparo1.gif bombU.gif
bombDL.gif bombL.gif bicho.gif Thumbs.db
bombR.gif bicho2.gif bicho1.gif disparo.gif
musica.wav bombUR.gif photon.wav misil.gif
nave.gif      

  Previous Page - A Scrolling playfield Current Page - 26 - Monster respawning   Next Page - Simple sound
  Return to the index of free Java tutorials  

(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted