• fullscreen
  • Bubble.pde
  • sketch_may23a_arrayOfObject.pde
  • class Bubble {
      color c;
      //velocity and accelerator
      float v, ac;
      //initial position
      float x0, y0;
      // dimension
      float di, d;
      
      Bubble(float tempX, float tempY){
        c = color(255);
        v = 0;
        ac = 0.1;
        x0 = tempX;
        y0 = tempY;
        di = 1;
     }
      
      void display(){
        fill(c);
        noStroke();
        ellipseMode(CENTER);
        if (di <= 80 && mousePressed){
        ellipse(x0, y0, di, di);
          di++;
        }else if (di== 80 || mousePressed == false){
           frozen[j] = di;
        }
      }
    
      void drop(float tempD){
          d = tempD; 
          ellipse(x0, y0, d, d);
          y0 = constrain(y0, 0, height - d/2);
          y0 = y0+v;
          v += ac;
          if (y0 >= height-d/2){
          v*=-0.9;      
         }
      }
    
      
    }
    
    Bubble [] myBubble = new Bubble[0];
    //frozen count number
    int j = -1;
    //frozen count array
    float [] frozen = new float[100];
    //initial position
    float xpos, ypos;
    
    
    
    void setup(){
      size (300,600);
      smooth();
      j = constrain (j, -1, 99);
    }
    
    void draw(){
     background(50);
     //display balls
     if(myBubble.length >0){
       for (int i=0; i<myBubble.length; i++){
         myBubble[i].display();
         myBubble[i].drop(frozen[i]);
         
       }
     }
    
    }
    
    void mousePressed(){
      xpos = mouseX;
      ypos = mouseY;
      //new object
      Bubble b = new Bubble(xpos, ypos);
      //append array
      myBubble = (Bubble[]) append(myBubble, b);
    }
    void mouseReleased(){  
      j++;
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Shanfan Huang

    Ping Pong

    Add to Faves Me Likey@! 2
    You must login/register to add this sketch to your favorites.

    Click on the Canvas to drop the "Ping Pong". The longer you hold your mouse key, the bigger the Ping Pong is.

    This is my Lesson 4 Project, after learning the concept of object, class and array. I struggled for long hours to get the "display()" function right, but no luck. I wanted the Ping Pong to keep the size after being generated by the mousePress(), and not to be affected by the next mousePress(). But instead the "display()" showing a flicker every time when the mouse is pressed.

    I found myself lacking of the programing process. I can't get the logic right before I sketch. All my lin

    blindfish
    24 May 2010
    You could add a boolean property to your class - e.g. 'newBall' - and set this to true in the constructor. Then in void mouseReleased() you'd add some code to set the property of the latest ball to false. You could then use this property to determine when to stop growing the ball in the display method... In fact this would also allow you to get rid of the 'frozen' array (which will trigger an arrayOutOfBounds exception eventially): you'd simply move the drop code into an else clause:

    void display() {
    if(newBall) {
    // make ball grow
    }
    else {
    // make ball drop
    }

    }
    Shanfan Huang
    25 May 2010
    Hey thanks a lot for tipping! I rewrote my display(), but it doesn't seem to stop the ball from growing once it starts to drop...
    How can I write a statement to ask the size variable "di" stop ++ in the "else{}" ?
    Here's the new code:

    void display(){
    fill(c);
    noStroke();
    ellipseMode(CENTER);
    ellipse(x0, y0, di, di);

    if (newBall == true){
    di++;
    di = constrain (di, 1, 50);
    } else {
    y0 = constrain(y0, 0, height - di/2);
    y0 = y0+v;
    v += ac;
    if (y0 &gt;= height-di/2){
    v*=-0.95;
    }
    }
    }

    Thanks again!
    Shanfan Huang
    25 May 2010
    The "frozen[]" did stop the ball from growing when the ball was dropping.
    Now that it is removed, the balls are growing as long as the mouse pressed, regardless it is dropping or being created...
    blindfish
    25 May 2010
    Easier for me to explain in code. See:

    http://www.openprocessing.org/visuals/?visualID=9932

    Basically you were making things too complicated for yourself. The addition of the boolean property simplifies things considerably... Note that I've made some other subtle changes to your code - e.g. changed 'v' to 'vy': you may want to add 'vx' later ;)
    You need to login/register to comment.