This is a "tower defense" game. I have a class called soldier.
if(wave1==true){
for(int i=0;i<(wave*10);i++){
handler.addObject(new soldier(getX(), getY(), ID.soldier, handler));
try {
// System.out.println(i);
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
wave++;
wave1=false;
try {
// System.out.println(i);
Thread.sleep(WT);
} catch (InterruptedException e) {
}
}
Every time this code is called is the soldier gets spawned 10 times for every wave, but I have one issue. I would like the tower 1 to kill the furthest soldier in range. The code for the killing:
private void collision() {
for(int i=0; i< Handler.object.size();i++){
GameObject tempObject = Handler.object.get(i);
if(tempObject.getID()== ID.ST1){
if(getBounds().intersects(tempObject.getBounds())){
Hsol=(Hsol-1);
if(Hsol<=0){
handler.removeObject(this);
}
}
}
}
}
So I know I need to do a distance formula to find which ones closer, but I'm not sure how to do that.
if(wave1=true)
. You probably wantif (wave1 == true)
, or much better:if (wave1)
. I would also try to choose much better names: having a boolean wave1 and an integer wave is quite confusing. WT, Hsol, ID don't respect naming conventions, don't mean anything, and make your code very hard to read, even for you. – JB Nizet