How can I continuously move these 'bullets' when I create another bullet object?

Viewed 68

I just started learning Java and recently learned the Timer class. I'm trying to apply what I have learned so far into making this project where a spaceship shoot bullets. I have all the controls working but I'm having trouble with the previous bullets stopping midway when I create another bullet object. I've been searching since yesterday but I can't seem to find the right keywords. I think I understand why it stops but how can I make the bullets continue to move?

I have this GamePanel class added in my JFrame.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int panelWidth = 500;
    int panelHeight = 500;
    
    JLabel spaceship;
    int shipWidth = 50;
    int shipHeight = 50;
    int shipX = 0;
    int shipY = panelHeight - shipHeight; // BOTTOM of GamePanel
    int shipXVelocity = 5;
    
    JLabel bullet;
    int bulletWidth = 10;
    int bulletHeight = 25;
    int bulletX;
    int bulletY;
    int bulleyYVelocity = 7;
    
    boolean left,right = false;
    
    Timer timer;
    
    GamePanel() {
        
        timer = new Timer(10,this);
        timer.start();
        
        this.setPreferredSize(new Dimension(panelWidth,panelHeight));
        this.setLayout(null);
        this.setBackground(Color.black);
        this.setFocusable(true);
        this.addKeyListener(this);
        
        spaceship = new JLabel();
        spaceship.setBounds(shipX, shipY, shipWidth, shipHeight);
        spaceship.setBackground(Color.green);
        spaceship.setOpaque(true);
        
        this.add(spaceship);
        
    }

    void shoot() {
        
        bulletX = shipX + (shipWidth / 2) - (bulletWidth / 2); // CENTER-TOP of spaceship
        bulletY = shipY;
        
        bullet = new JLabel();
        bullet.setBounds(bulletX, bulletY, bulletWidth, bulletHeight);
        bullet.setBackground(Color.white);
        bullet.setOpaque(true);

        this.add(bullet);
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
        switch(e.getKeyChar()) {
        case 'a' : left = true;
        break;
        case 'd' : right = true;
        break;
        }
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        
        switch(e.getKeyCode()) {
        case 10 : shoot();
        break;
        }
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
        switch(e.getKeyChar()) {
        case 'a' : left = false;
        break;
        case 'd' : right = false;
        break;
        }
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(left == true && shipX > 0) shipX -= shipXVelocity;
        if(right == true && shipX < panelWidth - shipWidth) shipX += shipXVelocity;
        spaceship.setLocation(shipX, shipY);
        
        if(bullet != null) { // Move if a bullet exist
            bulletY -= bulleyYVelocity;
            bullet.setLocation(bulletX, bulletY);
        }
        
        if(bulletY < 0) this.remove(bullet);;
        
    }
    
}

Output:

bullets stopping when I shoot another bullet

1 Answers

There are 2 possible solutions:

  1. Multi Threading: For every bullet you fire, open new timer who work just for this bullet until the bullet should stop, like when he hits something.
  2. Create a list for all the bullets: Create a list of bullet, and every time you fire a new bullet add it to the list without override the previous bullets. Then, in your actionPerformed method run with loop through the whole list, update every bullet there and remove from the list the bullets who should disappear.

I think that the second solution is better for your game, which haven't too much calculation is his objects movement.

Related