1

I want to create a function that can generate two balls that rise at different speeds. I used the parameters of the speed in which it rises called change and another parameter of its x coordinates called x, so that they don't originate from the same place. This is a code in processing, so the ellipse, size, fill, stroke, setup and draw functions are built in.

If I call the function, it works, and the ball moves according to the speed that I set. However, if I call the risingball() function twice, two balls are generated, but both move at the speed that is called with the second executed function. In this case, two balls were generated at x coordinates 150 and 450. The first ball was supposed to move at speed y-10, and the second ball at speed y-5, but both are moving at y-5.

Therefore, they move, but the whole purpose of the code which is making it change doesn't work.

void setup(){
  size(600,600);
}
float y = 600;

 void risingball(float change, float x){
  noStroke();
  fill(30,0,30);
  y = y-change;
  ellipse(x,y, 50,50);
}
  void draw(){
  background(255);
  risingball(10, 450);
  risingball(5, 250);
}
George Profenza
  • 50,687
  • 19
  • 144
  • 218

1 Answers1

0

Even though you're using the function twice, that function modifies the same one y variable:

risingball(10, 450); // modifies y first
risingball(5, 250);  // modifies the same y again (when it shouldn't)

Because you want to move two balls at independent speeds you all need two independent y variables (one for each ball).

You could use two independent y variables but you'd also need to modify the function so it doesn't change the same data. One option could be using a y argument and returning the modified data:

float y1 = 600;
float y2 = 600;

void setup(){
  size(600,600);
}

 float risingball(float change, float x, float y){
  noStroke();
  fill(30,0,30);
  y = y-change;
  ellipse(x,y, 50,50);
  return y;
}
  void draw(){
  background(255);
  y1 = risingball(10, 450, y1);
  y2 = risingball(5, 250, y2);
}

Another variant might be to simply use the function to render the ball, but keep the y variable updates outside the function:

float y1 = 600;
float y2 = 600;

void setup(){
  size(600,600);
}

 void risingball(float x, float y){
  noStroke();
  fill(30,0,30);
  ellipse(x,y, 50,50);
}
  void draw(){
  // update data
  y1 = y1 - 10;
  y2 = y2 - 5;
  //render
  background(255);
  risingball(450, y1);
  risingball(250, y2);
}

Yet another option is using classes, though I'm not sure if you had a chance to play with these yet. Just in case:

// RisingBall(float argX, float argY, float argChange, float argSize)
RisingBall ball1 = new RisingBall(450, 600, -10, 50);
RisingBall ball2 = new RisingBall(250, 600, -5, 50);

void setup(){
  size(600,600);
  noStroke();
  fill(30,0,30);
  
}

void draw(){
  background(255);
  ball1.updateAndDraw();
  ball2.updateAndDraw();
}

class RisingBall{
  
  float x;
  float y;
  float vy;
  
  float diameter;
  
  RisingBall(float argX, float argY, float argChange, float argSize){
    x = argX;
    y = argY;
    vy = argChange;
    diameter = argSize;
  }
  
  void updateAndDraw(){
    // update
    y += vy;
    // optional: reset Y
    if(y < -diameter){
      y = height;
    }
    // draw
    ellipse(x, y, diameter, diameter);
  }
  
}
George Profenza
  • 50,687
  • 19
  • 144
  • 218