Reputation: 13
I have the problem that my while loop ends although it only executed once where is my mistake?
public class ear {
private ArrayList<Point> Master = new ArrayList<Point>();
private ArrayList<Point> Shot = new ArrayList<Point>();
public ear() {
addMaster();
addShot();
if (Shot.get(0).y == Master.get(0).y) {
while (Shot.get(0).x > Master.get(0).x) {
System.out.println("MasterY: " + Master.get(0).x);
System.out.println("ShotY: " + Shot.get(0).x);
moveShot();
}
}
}
public void moveShot() {
Point p = new Point();
for (int i = 0; i < Shot.size(); i++) {
Shot.get(i).x = p.x;
p.x -= 10;
p.y = 5;
Shot.set(i, p);
}
}
}
An object of ear gets created in the main class In move shot i get the shot value x and subtract 10 of it.
Upvotes: 1
Views: 72
Reputation: 1612
In the moveShot()
method, you create a Point
that (by default) is at (0, 0)
Then, you iterate through all of your Shots
, setting their x value to 0. Then, the x value is no longer greater than your Master
x value, so the loop ends
Upvotes: 0
Reputation: 131436
1) this is useless as it is always true:
if (Shot.get(0).y == Shot.get(0).y)
2) Here Point p = new Point(); Shot.get(i).x = p.x;
makes no sense as you assign a new Point
to Shot.get(i).x
. If you want to have a intermediary value Point
to store the current Shot
and change its value, you should rather do the contrary :
p.x = Shot.get(i).x - 10;
p.y = Shot.get(i).y + 5;
and then set p
as the new value of the current Shot
:
Shot.set(i, p);
3) At last, if x
and y
are public fields of Shot
you could directly change them without using a intermediary Point
object:
public void moveShot() {
for (int i = 0; i < Shot.size(); i++) {
Shot.get(i).x -= 10;
Shot.get(i).y += 5;
}
}
It is not very advised to have instance public fields but in games it is sometimes used.
Upvotes: 1