# 2D shooting problem on Java using arrayList

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
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;
}


Yes, stop changing the missile's direction whenever you fire a new shot:

for (int i = 0; i < shots.size(); i++) {
// set shot's direction according to that of player's
shots.get(i).setDir(player.faceDirection); //reset the direction for each missile


You're looping through each shot and re-setting its direction to that of the players. If you don't want the shots to change direction, remove that line.

Just create the shot and add it:

Shot newShot = new Shot(this);
newShot.setDir(player.faceDirection);


i acheived this with a program i wrote called "rotating" availible for referance at Nathan's Computer Knowledge

how i acheived this was to attach a line to the top of my image (the x, y coordinate) to the center of the image using the getWidth() getHeight() methods. that way when i rotated the image which was a space ship, the line would point in the dirrection of the front of the space ship, which could later be used to tell which dirrection to fire the missiles, so the animation will display a space ship firing missiles.

you also need to rotate your image and make your ship or whatever front of the image whatever your image is the front needs to be pointing to the top right of the or i mean the top of top left.

this teqnique as far as i know only works with images rotating that are not moving , it may work with space ships but your ship might be deformed haha but i think it would look fine.

the next thing you need to do is get the slope of the lines, and figure out which dirrection it is facing.

i achieved something similar to this at point a to point page

how now that i think about it, i might be trying to answer the wrong question. but if you ever need to get a image or an object from anywhere on the screen to anywhere on the screen i would really reccomend that point a to point b page listed above. it is difficult to explain the answer but i have acheived this myself for a program that didnt have user input, in other words a "stationary" object rotating and shooting in a 360' dirrection , displaying animation as a cannon shooting cannon ball s , that was the first or the second link i listed the first one, which may be somewhat helpful to your question.

to clarify, you need to (as described above) rotate your image in a paint program (disclaimer: your image will show as rotated to the left 50%), add a line line2d.double(yourimage.getx(), yourimage.gety(),yourimage.getwidth() / 2, yourimage.getheight() / 2);, from the top left (x, y) to the center (as described above), then you need to get the slope of the line, figure out which dirrection your line is facing in an 8 quadrent circle, which i have done (the last two steps) i have done at the page listed above, the first page "Nathan's computer knowledge" you may use my program "rotating" as a reference to achieve the last two steps which are long and complicated, but if you look at my program you should be able to see how i did it, and the page has a runnable jar zip folder attached working on windows for download demonstration visual and the source and the source and images are included.

i think that is it, you could have rotation code set up to a key listener as well.

good luck to you, if you have any questions or comments or would like to contact me, please reply via email that is listed on the about me page or should be listed.

Best regards, Nathan Nelson

one of the links i was trying to post it looks like it got the wrong url acctually i think i might have fixed it its called "rotating" on the nathanscomputerknowledge website it should be listed in the navigation onthe left side of the screen. acctually it is called "rotating cannons shooting cannon balls in a runnable jar"

also: when you get the slope of the line and the quadrent number, you have to increments the x or the y value depending on which quadrent you are in, and the y and the x value by the slope of the line. keeping looking at "rotating" if you get lost and watch the program in your ide to see what it does, then you will know how to do what it's doing by looking at the code, which may solver your problem or similar problems.

take note: this teqnique works in a 360' radius not just a 8 quadrent vector (circle)