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
bullet.java
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.
player.class
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++)
{
....paint(g);
}
}
}
this line is way wrong. i dont now how to print arraylist<bullet>. and display image on screen.
....paint(g);