so i got finally done with make my player move right and left, and 1st level. now i want to make my player shoot bullets. i kind of now logic behind it but i need some help. and please let me know if you have a better method of bullet class.

my plan:
1)create bullet class - done
2)when user hit space bar add bullet in arraylist - i think iam done not sure
3)print arraylist in paint method - need help


public class bullet
    private int x, y;
    private int dx = 10;
    private int width = 10;
    private int height = 10;
    private boolean dead = true;  //dead mean you can not see the bullet. so when game start no bullet
    private int bullet_limit = 50;  //limit how many times can player shoot
    private int bullet_range = 200; //set how far a bullet can go.
    private BufferedImage bullet_image;  //get image of bullet             


       //move my bullet
    public void SHOOT_MOVE()  //move bullet
        x += dx;

        if(x > bullet_range)  //kill bullet if it get above bullet range
            dead = false;

    //draw image of bullet. i didnt added code for adding this image but its works fine
    public void paint(Graphics g)
        g.drawImage(bullet_image, x, y, width, height, null);

    }//end of paint method

alright so far good. i created a bullet and i am moving it right.
now i am store the bullet in arraylist where ever user hit space bar.


public class player {
    bullet bullet_class;
    ArrayList<shoot> store_bullets = new ArrayList<shoot>(); //store bullets

    //set and get methods
    public ArrayList<shoot> getBULLETS()
    { return store_bullets; }
    public void setBULLETS(shoot value) //add bullets
    { store_bullets.add(value); }


   public void KEYS()  //i am calling this method in main(actionPerformed) method. so it keep updateing when i hit space bar
    if(space) //boolean var. when user hit space bar
      bullet_class = new bullet(x, y);    //create bullet   
      store_bullets.add(bullet_class);    //add in arraylist
      bullet_class.SHOOT_MOVE();          //update bullet move

now i want to print arraylist<bullet> in main method.

public class main ...{
    public void paintComponent(Graphics g)

    for(int i = 0; i < player_class.store_bullets.size(); i++)


this line is way wrong. i dont now how to print arraylist<bullet>. and display image on screen.

4 Years
Discussion Span
Last Post by JamesCherrill

Here's the usual way to do it:
in the bullet class have an update method (where you do x+=dx; etc)and a paint method.
Maintain a list of all the current active bullets, and make that list available to the main class.
In the main timer method call each bullets update method.
in the main paint method, call each bullets' paint method.

The simplest way to share the list of active bullets is to have a public static ArrayList<Bullet> in the Bullet class so any other class can access that list to iterate through the bullets.

If you are going to have different types of things moving around then you should consider having them all implement the same interface or inherit from the same abstract superclass (ie the update and print methods), then all you need is a single list for everything that moves.. j


i have a question on arraylist? what is the reason for storing bullets. i am using player without storeing and i can use collision and other function on it.

is it so if i shoot my 1st bullet than i can use function on it but if i shoot my 2nd bullet. than java forget about 1st bullet and it will use collsion function on 2nd bullet, not 1st bullet?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.