C
caleb
I'm making a space invader-like game, and for one method
getLocationOfNearestAlien, which lasers use in part to see if they are
touching an alien, I need some help.
public NearestAlienInfo getLocationOfNearestAlien(double x, double y) {
if (aliens.isEmpty()) {
return new NearestAlienInfo((int) x, (int) y, 100000000);
}
double closestDistanceSoFar = 100000000;
Alien closestAlien = null;
double d =0;
double e =0;
for(int i=0; i<aliens.size(); i++) {
Alien a = (Alien) aliens.elementAt(i);
if(a.contains(ufos)) {
for(int z=0; z<ufos.size(); z++) {
Ufo u = (Ufo) ufos.elementAt(i);
d = u.getSizeX() /2;
e = u.getSizeY() /2;
}
}
else if(a.contains(smartAliens)) {
for(int z=0; z<smartAliens.size(); z++) {
SmartAlien s = (SmartAlien)
smartAliens.elementAt(i);
d = s.getSizeX() /2;
e = s.getSizeY() /2;
}
}
// doubles b and c should point to a pixel in the center of the alien
double b = a.getX() + d;
double c = a.getY() + e;
//this is the actual ammt for Ufo before I decided I would need to make
it more flexible for more alien types.
//double b = a.getX() + 32; //that's half of the Ufo x
pixels
//double c = a.getY() + 18.5; // and half of the Ufo y
pixels
double dx = x - b;
double dy = y - c;
double distance = Math.sqrt(dx*dx + dy*dy);
if (distance < closestDistanceSoFar) {
closestDistanceSoFar = distance;
closestAlien = a;
}
}
return new NearestAlienInfo(closestAlien.getX(),
closestAlien.getY(),
closestDistanceSoFar);
}
There are different aliens. And each has a different size image that
I'm using. The Alien class is the basic class that the class Ufo and
SmartAlien extend. The Vector aliens includes all aliens but I want the
if statements to work so that if the a object is also part of ufos (not
just part of aliens), then it will add it's pixel size divided by two
and then add that number to where it is so you end up with a pixel
right in the middle of the Ufo. If it is not a Ufo I want it to check
if it is a SmartAlien and so on with all the aliens I have made. I
experimented with contains and equals but I couldn't get either to work
if they are even the right way to do it. So if anyone could help me and
perhaps show me some source code for how I should do it that would be
fantastic. I'm new to java(and programming in general) though so I
don't know all of the lingo yet. Also if anyone had any good links to
starting java game programmers that would be great.
getLocationOfNearestAlien, which lasers use in part to see if they are
touching an alien, I need some help.
public NearestAlienInfo getLocationOfNearestAlien(double x, double y) {
if (aliens.isEmpty()) {
return new NearestAlienInfo((int) x, (int) y, 100000000);
}
double closestDistanceSoFar = 100000000;
Alien closestAlien = null;
double d =0;
double e =0;
for(int i=0; i<aliens.size(); i++) {
Alien a = (Alien) aliens.elementAt(i);
if(a.contains(ufos)) {
for(int z=0; z<ufos.size(); z++) {
Ufo u = (Ufo) ufos.elementAt(i);
d = u.getSizeX() /2;
e = u.getSizeY() /2;
}
}
else if(a.contains(smartAliens)) {
for(int z=0; z<smartAliens.size(); z++) {
SmartAlien s = (SmartAlien)
smartAliens.elementAt(i);
d = s.getSizeX() /2;
e = s.getSizeY() /2;
}
}
// doubles b and c should point to a pixel in the center of the alien
double b = a.getX() + d;
double c = a.getY() + e;
//this is the actual ammt for Ufo before I decided I would need to make
it more flexible for more alien types.
//double b = a.getX() + 32; //that's half of the Ufo x
pixels
//double c = a.getY() + 18.5; // and half of the Ufo y
pixels
double dx = x - b;
double dy = y - c;
double distance = Math.sqrt(dx*dx + dy*dy);
if (distance < closestDistanceSoFar) {
closestDistanceSoFar = distance;
closestAlien = a;
}
}
return new NearestAlienInfo(closestAlien.getX(),
closestAlien.getY(),
closestDistanceSoFar);
}
There are different aliens. And each has a different size image that
I'm using. The Alien class is the basic class that the class Ufo and
SmartAlien extend. The Vector aliens includes all aliens but I want the
if statements to work so that if the a object is also part of ufos (not
just part of aliens), then it will add it's pixel size divided by two
and then add that number to where it is so you end up with a pixel
right in the middle of the Ufo. If it is not a Ufo I want it to check
if it is a SmartAlien and so on with all the aliens I have made. I
experimented with contains and equals but I couldn't get either to work
if they are even the right way to do it. So if anyone could help me and
perhaps show me some source code for how I should do it that would be
fantastic. I'm new to java(and programming in general) though so I
don't know all of the lingo yet. Also if anyone had any good links to
starting java game programmers that would be great.