I'm trying to shoot missiles according to a direction which a player is facing at. A missile goes towards right iff the player is facing towards right and shoot for instance. It is working but when missiles which moving towards for example left still on a screen and new ones are shot towards right, the missiles which moving towards left turn back and start moving towards right as well. I'm trying to treat each missile as an object for this shooting problem but seems not working. Is there any way to fix this?
In MainPanel class I have followings;
public void run() {
while (true) {
move(); // move the player
player.update(); // update the player's conditions
if (firePressed) {
fire();
}
if (shots.size() > 0) {
for (int i = 0; i < shots.size(); i++) {
shots.get(i).move();// get a first shot and move it
}
}
repaint();
}
and
private void fire() {
// create a shot and store it in arraylist
shots.add(new Shot(this));
for (int i = 0; i < shots.size(); i++) {
// set shot's direction according to that of player's
shots.get(i).setDir(player.faceDirection);
if (shots.get(i).isInStorage()) {
Point posX = player.getPos();
shots.get(i).setPos(posX.x - player.getWidth() / 8, posX.y);
return; // break?
}
// shots.get(i).
}
}
in Shot class I have
// Move the shot
public void move() {
if (isInStorage()) { // if the shot are in the storage
return; // do nothing
}
if (direction == LEFT){
x -= SPEED;
} else if (direction == RIGHT){
x += SPEED;
}
if (x < 0 || x > panel.WIDTH) { // when a shot went outside the screen range
panel.shots.remove(0);
}
}
// Set the shot direction and return its direction
public int setDir(int dir){
if (dir == LEFT){
direction = LEFT;
} else if (dir == RIGHT){
direction = RIGHT;
}
return direction;
}