Basically, I am creating a top down shooter in java, and for the bullets there is a bullet object with all the properties and update methods and stuff. I hava decided to use an array list to store the bullets in once the mouse is pressed and an instance of the object has been created. The problem is that I do not know how to identify elements in an array list. Here is a snippet of some of the code when i was using just a simple array:
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
pressedX = e.getX();
pressedY = e.getY();
bullets[bulletCount] = new Bullet(player.x, player.y));
double angle = Math.atan2((pressedY - player.y),(pressedX - player.x));
bullets[bulletCount].dx = Math.cos(angle)*5;
bullets[bulletCount].dy = Math.sin(angle)*5;
bulletCount++;
}
Any help is greatly appreciated! Thanks in advance!
You could just change anything like this:
to
However, in the code you’ve given, we can do better.
So:
Now that’s still accessing fields in the bullet directly, which doesn’t seem like a very good idea to me. I would suggest that you either use properties for
dxanddy– or possibly a single property taking aVelocity(which would basically be a vector of dx and dy) – or you make that part of the constructor: