Getting 2 arrays of objects to interact.
in
Programming Questions
•
1 years ago
I'm trying to make a space invaders type program. So I have an array of bullets from the ship and an array of enemies. I have a boolean function in the Bullet class, but I'm not sure how to test it against all of the other enemies. Sorry if this seems confusing. Here's my code. (O line 63, where i wrote "
HERE IS WHERE I NEED HELP!!" is where i think i need help. haha)
- Timer timer;
- Bullet[] bullets;
- int xBullet, yBullet;
- int r;
- int totalBullets = 0;
- Enemy[] enemies;
- int totalEnemies = 0;
- void setup() {
- size(600, 600);
- smooth();
- background(0);
- enemies = new Enemy[1000];
- bullets = new Bullet[5000];
- timer = new Timer(300); // Create a timer that goes off every 2 seconds
- timer.start(); // Starting the timer
- }
- void draw() {
- if (frameCount == 10) {
- // delay(1000);
- }
- fill(0, 10);
- rect(0, 0, 600, 600);
- //ENEMIES
- // Check the timer
- if (timer.isFinished()) {
- // Deal with enemies
- // Initialize one enemy
- enemies[totalEnemies] = new Enemy();
- // Increment totalEnemies
- totalEnemies ++ ;
- // If we hit the end of the array
- if (totalEnemies >= enemies.length) {
- totalEnemies = 0; // Start over
- }
- timer.start();
- }
- // Move and display all enemies
- for (int i = 0; i < totalEnemies; i++ ) {
- enemies[i].move();
- enemies[i].display();
- }
- //BULLETS
- bullets[totalBullets] = new Bullet();
- if (totalBullets >= bullets.length) {
- totalBullets = 0;
- }
- for (int i = 0; i <= totalBullets; i++) {
- bullets[i].shoot();
- bullets[i].display();
- println("here");
- if (bullets[i].hit(enemies[HERE IS WHERE I NEED HELP!!])) {
- println("HIIIIT");
- }
- //println(bullets[0].yBullet);
- }
- }
- void keyReleased() {
- bullets[totalBullets].shoot();
- totalBullets++;
- println("keyPressed " + "Bullet #" + totalBullets);
- }
- class Bullet {
- float x, y;
- float r;
- Bullet() {
- y = 580;
- x = mouseX;
- r = 5;
- }
- void display() {
- ellipseMode(CENTER);
- ellipse(x, y, r, r*2);
- }
- void shoot() {
- y -= 10;
- }
- // A function that returns true or false based on
- // if the catcher intersects a raindrop
- boolean hit(Enemy d) {
- // Calculate distance
- float distance = dist(x,y,d.x,d.y);
- // Compare distance to sum of radii
- if (distance < r + d.r) {
- return true;
- } else {
- return false;
- }
- }
- }
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 10-10: The rainEnemy catching game
- class Enemy {
- float x,y; // Variables for location of rainEnemy
- float speed; // Speed of rainEnemy
- color c;
- float r; // Radius of rainEnemy
- Enemy() {
- r = 8; // All rainEnemys are the same size
- x = random(width); // Start with a random x location
- y = -r*4; // Start a little above the window
- speed = random(1,5); // Pick a random speed
- c = color(50,100,150); // Color
- }
- // Move the rainEnemy down
- void move() {
- // Increment by speed
- y += speed;
- }
- // Check if it hits the bottom
- boolean reachedBottom() {
- // If we go a little beyond the bottom
- if (y > height + r*4) {
- return true;
- } else {
- return false;
- }
- }
- // Display the rainEnemy
- void display() {
- // Display the Enemy
- fill(c);
- noStroke();
- for (int i = 2; i < r; i++ ) {
- ellipse(x,y + i*4,i*2,i*2);
- }
- }
- // If the Enemy is caught
- void caught() {
- // Stop it from moving by setting speed equal to zero
- speed = 0;
- // Set the location to somewhere way off-screen
- y = - 1000;
- }
- }
1