Objects in arrays

So I am making somekind of game that you have airplanes shooting meteorites. But I need some help with Array in objects to add and remove that meteorite object in my array. And i'm using the "class" function.

Answers

  • Plane plane;

    PImage plane_0; PImage plane_1; PImage plane_2; PImage plane_3; PImage plane_4; PImage plane_5; PImage plane_shoot_0; PImage plane_shoot_1; PImage plane_shoot_2;

    void setup() { imageMode(CENTER);

    plane_0 = loadImage("plane_0.png"); plane_1 = loadImage("plane_1.png"); plane_2 = loadImage("plane_2.png"); plane_3 = loadImage("plane_3.png"); plane_4 = loadImage("plane_4.png"); plane_5 = loadImage("plane_5.png"); plane_shoot_0 = loadImage("plane_shoot_0.png"); plane_shoot_1 = loadImage("plane_shoot_1.png"); plane_shoot_2 = loadImage("plane_shoot_2.png");

    plane = new Plane(); size(1000, 650); }

    void draw() { background(0); //plane.display(); //plane.movement(); }

    class Meteorite { PVector location; PVector velocity;

    Meteorite() { location = new PVector(random(0, width), 0); velocity = new PVector(); }

    void display() { fill(255, 0, 0); ellipse(location.x, location.y, 100, 100); } }

    class Plane { PVector location; PVector velocity; float force = 0.1; int animate;

    Plane() { location = new PVector(width/2, height/2); velocity = new PVector(0, 0); }

    void display() { animate++;

    if(animate >= 0 && animate <= 4) {
      image(plane_0, location.x, location.y);
    }
    
    if(animate >= 5 && animate <= 9) {
      image(plane_1, location.x, location.y);
    }
    
    if(animate >= 10 && animate <= 14) {
      image(plane_2, location.x, location.y);
    }
    
    if(animate >= 15 && animate <= 19) {
      image(plane_3, location.x, location.y);
    }
    
    if(animate >= 20 && animate <= 24) {
      image(plane_4, location.x, location.y);
    }
    
    if(animate >= 25 && animate <= 29) {
      image(plane_4, location.x, location.y);
    }
    
    if(animate >= 29) {
      animate = 0;
    }
    

    }

    void movement() { if(keyPressed == false) { if(velocity.x >= 0.001) { velocity.x = velocity.x - force; }

      if(velocity.x <= -0.001) {
        velocity.x = velocity.x + force;
      }
    }
    
    if(location.x >= 0 + 64) {
      if(keyCode == LEFT) {
        if(keyPressed == true) {
          velocity.x = velocity.x - force - force;
        }
        location.x = location.x + velocity.x;
      }
    } else {
      velocity.x = 0;
    }
    
    if(location.x <= width - 64) {
      if(keyCode == RIGHT) {
        if(keyPressed == true) {
          velocity.x = velocity.x + force + force;
        }
        location.x = location.x + velocity.x;
      }
    } else {
      velocity.x = 0;
    }
    

    } }

  • What do you mean with that. I don't see any use of that what I can use in my code.

  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Kf

Sign In or Register to comment.