Eggs disappearing in mid-air as they are going down

eggs disappear midway as they are going down, please send help! idk if its the timer or array problem, but if anyone can detect why it disappears at random times, then please send the correct code

 PImage pan;
 PImage egg;
 PImage goldegg;
 PImage rottenegg;

 Pan catcher;
 Timer timer;
 Normal[] normal; 
 Golden[] golden;
 Rotten[] rotten;
 int totalNormal = 0;
 int totalGolden = 0;
 int totalRotten = 0;

 void setup() {
   size(500, 500);
   imageMode(CENTER);

   catcher = new Pan(20); 
   normal = new Normal[10];
   golden = new Golden[5];
   rotten = new Rotten[5];
   timer = new Timer(3000);
   timer.start();
 }

 void draw() {
   background(250, 237, 235);

   catcher.setLocation(mouseX, 450); 
   catcher.display(); 


   if (timer.isFinished()) {

     normal[totalNormal] = new Normal();
     totalNormal++ ;

     golden[totalGolden] = new Golden();
     totalGolden++;

     rotten[totalRotten] = new Rotten();
     totalRotten++;

     if (totalNormal >= normal.length) {
       totalNormal = 0; 
     }

     if (totalGolden >= golden.length) {
      totalGolden = 0;
     }

     if (totalRotten >= rotten.length) {
       totalRotten = 0;
     }

     timer.start();
   }


   for (int i = 0; i < totalNormal; i++ ) {
     normal[i].move();
     normal[i].display();

     if (catcher.collision(normal[i])) {
       normal[i].caught();
     }
   }

   for (int i = 0; i < totalGolden; i++) {
    golden [i].move();
    golden[i].display();

    if(catcher.collision(golden[i])) {
      golden[i].caught();
    }
   }

   for (int i = 0; i <totalRotten; i++){
     rotten[i].move();
     rotten[i].display();

     if(catcher.collision(rotten[i])) {
       rotten[i].caught();
     }
   }
 }
 class Golden {
   float x, y, r;
   float speed;

   Golden() {
     r = 10;
     x = random(width);
     y = -20;
     speed = 4;
   }

   void move() {
     y += speed;
   }


 //  boolean atBottom() {

 //    if (y > height + r) { 
 //      return true;
 //    } else {
 //      return false;
 //    }
 //  }


   void display() {
     goldegg = loadImage ("GoldenEgg.png");

     for (int i = 2; i < r; i++ ) {
     image(goldegg, x, y);
     }
   }


   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 class Normal {
   float x, y, r;
   float speed;

   Normal() {
     r = 10;
     x = random(width);
     y = -20;
     speed = random(1, 2);
   }

   void move() {
     y += speed;
   }


   //boolean atBottom() {

   //  if (y > height + r) { 
   //    return true;
   //  } else {
   //    return false;
   //  }
   //}


   void display() {
     egg = loadImage ("RegularEgg.png");

     for (int i = 2; i < r; i++ ) {
     image(egg, x, y);
     }
   }


   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 float r; 
   float x, y; 

   Pan(float tempR) {
     r = tempR;
     x = 0;
     y = 0;
   }

   void setLocation(float tempX, float tempY) {
     x = tempX;
     y = tempY;
   }

   void display() {
    pan = loadImage ("Pan.png");
    image(pan, x, y);

   }


   boolean collision(Normal n) {
    float d = dist(x, y, n.x, n.y); 

     if (d < r + n.r) { 
       return true;
     } else {
       return false;
     }
   }

   boolean collision(Golden g) {
    float d = dist(x, y, g.x, g.y);

     if (d < r + g.r) {
       return true;
     } else {
       return false;
    }
   }

   boolean collision(Rotten o) {
     float d = dist(x, y, o.x, o.y);

     if (d < r + o.r) {
       return true;
     } else {
       return false;
     }
   }
  }
 class Rotten {
   float x, y, r;
   float speed;

   Rotten() {
     r = 10;
     x = random(width);
     y = -20;
     speed = 3;
   }

   void move() {
     y += speed;
   }


   //boolean atBottom() {

   //  if (y > height + r) { 
   //    return true;
   //  } else {
   //    return false;
   //  }
   //}


   void display() {
     rottenegg = loadImage ("RottenEgg.png");

     for (int i = 2; i < r; i++ ) {
     image(rottenegg, x, y);
     }
   }

   void caught() {
    speed = 0; 
    y = -1000;
   }
 }
 class Timer {

   int savedTime; 
   int totalTime; 

   Timer(int tempTotalTime) {
     totalTime = tempTotalTime;
   }


   void start() {

     savedTime = millis();
   }


   boolean isFinished() { 

     int passedTime = millis()- savedTime;
     if (passedTime > totalTime) {
       return true;
     } else {
       return false;
     }
   }
 }

Answers

  • I don't know what happened to the other discussion I was writing a reply to. Some of what I wrote applies here too. Here's what I had:


    Well, your code has some problems.

    The biggest issue I see is a lack of nice formatting. Press Ctrl + t in the editor to automagically format your code.

    If you do that, then it should immediately become apparent that you are missing a stray closing brace. In fact, your Timer class is getting defined inside your Pan class! Add this closing brace in so your Pan class ends before your Timer class.

    The next issue I see is that you are loading the images inside the class definitions. Don't do this. To make sure that the images are only ever loaded once, put them inside the function that only ever runs once: setup().

    The display() method inside the normalEgg class has a loop in it. I have no idea why. You don't need a loop to draw the same image in the same place ten times. Just draw the image once?

    Anyway, actually looking at the collision method, it appears you have a pair of stray opening and closing braces in there that you don't need. Remove those.

    Looking at the collision method some more, we see that it looks fine! You are getting the distance from the center of the pan to the center of an egg. If that distance is less than some value, then there is a collision. That sounds good. There's no problem with it.

    But there is a problem. Your code has a bug. A bug happens when you have assumed your code does something that it dosen't actually do.

    This is a really sneaky bug too, and I suggest you take a minute here to try to find it yourself again before I clue you in to the problem.

    Last chance to solve it yourself. Here's a hint: Where is your pan? No really, where is your pan?

    The problem is that your code assumes your pan is at (pan.x, pan.y). But it isn't. It's drawn at (mouseX, 450), so where it appears to be has nothing to do with the x and y variables in the pan class! That is, even though the pan moves like you would like, if you use pan.x and pan.y as it's position, well, that position is always (0,0)!

    The best way to fix this is to modify the Pan class:

    class Pan {
      float x, y, r;
    
      Pan() {
        r = 30;
        x = mouseX;
        y = 450;
      }
    
      void display() {
        x = mouseX;
        image(pan, x, y);
      }
    }
    

    Now pan.x and pan.y are updated when they need to be, and the collision function should work properly. Try it! Does it?

  • This code is EVEN WORSE when it comes to dealing with images! Where did you get it from? Why assume it's better? It's worse!

  • edited April 2018

    I added the class pan and changed it to the revised one you gave, but it is now giving me errors that there are things that do not exist such as Pan(int), collision(Normal), variables x,y and r

  • correct the errors then

    it's your code

    then post your entire code

Sign In or Register to comment.