Welcome to Dream.In.Code
Become a Java Expert!

Join 150,049 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,706 people online right now. Registration is fast and FREE... Join Now!




Multiple Missiles -2D Shooter game

 
Reply to this topicStart new topic

Multiple Missiles -2D Shooter game

MikeAbyss
14 Jun, 2008 - 01:19 PM
Post #1

New D.I.C Head
Group Icon

Joined: 9 Jul, 2007
Posts: 9


Dream Kudos: 25
My Contributions
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!
User is offlineProfile CardPM
+Quote Post

pbl
RE: Multiple Missiles -2D Shooter Game
14 Jun, 2008 - 02:02 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
This

CODE

       if (e.getKeyCode() == e.VK_SPACE) {    
            missiles = new Missiles(ship.shipX + (ship.shipSize/3), ship.shipY);
            space = true;
        }      


creates a new Missiles and assigns it to the instance variable missiles in Space_Shooter

CODE

public class Space_Shooter extends Applet implements KeyListener, Runnable, FocusListener {

    Image offscreenImage, backGround;            // double buffering technique
    Graphics offscreenGraphics;  
        
    Missiles missiles;      // <---------------------------------


Sure that the paint() method will only redraw the one hold in the variable missiles
may-be you will have to make an array of Missiles object with a count of the number of missiles in the game

CODE

int nbMissiles = 0;
Missiles[] missiles = new Missiles[100];


now in your Listener

CODE

       if (e.getKeyCode() == e.VK_SPACE) {    
            missiles[nbMissiles++] = new Missiles(ship.shipX + (ship.shipSize/3), ship.shipY);
            space = true;
        }      


Then in your paint() method you will have to re-draw every missile

CODE

    for(int i = 0; i < nbMissibles; i++) {
       ... redraw miassiles[i]
    }


User is online!Profile CardPM
+Quote Post

MikeAbyss
RE: Multiple Missiles -2D Shooter Game
14 Jun, 2008 - 02:15 PM
Post #3

New D.I.C Head
Group Icon

Joined: 9 Jul, 2007
Posts: 9


Dream Kudos: 25
My Contributions
Thank-you helping me, it works perfectly now biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month