We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys, How can I write this checkAsteroidDeath() for both of these classes? Thanks so much
void drawAsteroids() {
for (int i=0; i<asteroids.size(); i++) {
asteroids.get(i).draw();
}
}
void drawBullets() {
for (int i=0; i<bullets.size(); i++) {
bullets.get(i).draw();
}
}
void checkAsteroidDeath() {
// for each asteroid
// check to see if it is touching any lasers
// if it is touching any lasers,
// remove() the laser
// remove() the asteroid
// add +10 to the score
}
public class Bullet {
float x;
float y;
float speed;
int size = 2;
float direction;
Bullet(float x, float y, float d) {
this.x = x;
this.y = y;
direction = d;
speed = 6;
}
float getX() {
return x;
}
float getY() {
return y;
}
public void updatePosition() {
x = x + (speed*cos(direction));
y = y + (speed*sin(direction));
}
public void draw() {
updatePosition();
fill(200, 100, 0);
ellipse(x, y, size, size);
}
}
public class Asteroid {
float x;
float y;
float speed;
float direction;
int size;
Asteroid( ) {
x = random(width);
y = random(height);
speed = 1.1;
direction = random(-1, 1);
size = 20;
}
void draw() {
updatePosition();
fill(0, 0, 255);
ellipse(x, y, size, size);
}
void updatePosition() {
x = x + (speed*cos(direction));
y = y + (speed*sin(direction));
}
float getX() {
return x;
}
float getY() {
return y;
}
}
Answers
Here's a simulation of Zombie Vs. Bullet I had here: 8-X
is there another better way of writing this code?
http://pastebin.com/jpmGhJTm
You're not following the backwards loop remove() rule there!!! :-w
And the best place to place a checkHit() kinda method is inside a class!
thanks. What does this part of the code mean?
if ((sq(aX - bX) + sq(aY-bY)) < 160) {
Fused my "Zombie Shooting Simulation" into your sketch, and made "Asteroids Vs. Bullets Sim". Check it out: B-)
http://studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest
Where did you get that algorithm from? Wasn't that in your PasteBin.com link btW? [..]
That's pretty much the same as my checkHit() method:
return sq(a.x - x) + sq(a.y - y) < a.RAD * a.RAD;
It gets whether 2 objects are intersecting within a circular area! *-:)
As a bonus, a CoffeeScript version: \m/