I'm creating a shooter game, and I'm having troubles with the missiles. My problem is that when I shoot one missile, it goes up as it should, but when I shoot another missile, the first one disappears, and the second one shots up again. My friend told me to create multiple instances of my missile class, but I don't know exactly how to do that. here is my code
CODE FOR APPLET
CODE
import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.event.*;
public class Space_Shooter extends Applet implements KeyListener, Runnable, FocusListener {
Image offscreenImage, backGround; // double buffering technique
Graphics offscreenGraphics;
final int SLEEPDELAY = 0; // slows down the program
//int x;
boolean suspended; // used to suspend the game
boolean running = false, space; // used to suspend the game, space for shooting
Ship ship;
Rocks rocks;
Life life;
Explosion explosion;
Missiles missiles;
public void init() {
setBackground(Color.black);
offscreenImage = createImage(getSize().width,getSize().height);
offscreenGraphics = offscreenImage.getGraphics();
addKeyListener(this);
addFocusListener(this);
ship = new Ship();
rocks = new Rocks();
life = new Life();
}
public void update (Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(offscreenImage, 0, 0, this); // draws the offline screen
offscreenGraphics.clearRect(0,0,getSize().width,getSize().height); // clear the offline screen
}
public void run() {
while(life.lives !=0){
try {
Thread.currentThread().sleep(SLEEPDELAY);
}
catch (InterruptedException e) {};
if(!suspended)
{
offscreenGraphics = life.drawLives(offscreenGraphics);
offscreenGraphics = ship.drawShip(offscreenGraphics);
offscreenGraphics = rocks.drawRocks(offscreenGraphics);
if (space){
offscreenGraphics = missiles.drawMissiles(offscreenGraphics); // if space = true draws missiles
}
if (rocks.rockX+ (rocks.rockSize/2) >= ship.shipX && rocks.rockX + (rocks.rockSize/2)<= ship.shipX + ship.shipSize) // collision detection
{
if (rocks.rockY + rocks.rockSize >= ship.shipY){
explosion = new Explosion(rocks.rockX, rocks.rockY);
explosion.drawExplosion(offscreenGraphics);
//rocks.reset(); // to use used later
//life.lifeLose(); // to use used later
}
}
}
running=false;
repaint();
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == e.VK_RIGHT) {
ship.right(true);
}
if (e.getKeyCode() == e.VK_LEFT) {
ship.left(true);
}
if (e.getKeyCode() == e.VK_SPACE) {
missiles = new Missiles(ship.shipX + (ship.shipSize/3), ship.shipY);
space = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == e.VK_RIGHT) {
ship.right(false);
}
if (e.getKeyCode() == e.VK_LEFT) {
ship.left(false);
}
if (e.getKeyCode() == e.VK_SPACE) {
space = missiles.decide();
}
}
public void keyTyped(KeyEvent e) {
}
public void focusGained(FocusEvent e) { // runs game
System.out.println("Focus Gained");
if(running == false){
suspended =false;
running = true;
(new Thread(Space_Shooter.this)).start();
}
else{
suspended = false;
}
}
public void focusLost(FocusEvent e) { // suspends game
System.out.println("Focus Lost");
suspended = true;
}
}
CODE FOR MISSILES
CODE
import java.awt.*;
import java.awt.Graphics;
public class Missiles {
Image missiles;
int missileX;
int missileY;
final int missileSize = 25;
public Missiles(int missileX, int missileY)
{
this.missileX = missileX;
this.missileY = missileY;
try
{ // try and load missile
missiles = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/Missile.gif"));
}
catch(Exception e){}
}
public Graphics drawMissiles(Graphics g){
if (missileY == 0 - missileSize){ // missile disappear after off screen
missileY = 400;
}
g.drawImage(missiles, missileX, missileY--, null); // draws the missile
return g;
}
public boolean decide(){ // currently unused function, used later
if (missileY == 0)
return false;
return true;
}
}
Thank-you for all your help!