Home > database >  MousePressed and mouseReleased to create objects
MousePressed and mouseReleased to create objects

Time:11-23

I have a ball class that creates a ball object. I want to created a ball object when the mouse is pressed, if the mouse is pressed and held the ball should follow the mouse(while the mouse is pressed). When the mouse is releases, the ball should stay at the current mouseX and mouseY, and no longer follow the mouse.

When the mouse is pressed again, another ball object is created and does the same as the first object and so on...A new ball object is created every time the mouse is pressed.

I have some code that creates an object, it follows the mouse and it puts on the canvas when I release the mouse. But if i press my mouse again the original ball goes back to the mouse. How do I "disengage" the ball object so i can make more ball objects that doesnt effect the position of the previously placed balls?.

I plan to use ArrayLists to make many objects .

class Ball {
  float xPos, yPos; 


  Ball(float xPos, float yPos) {
    this.xPos= xPos;
    this. yPos= yPos;
  }
  void drawBall() {
    ellipse(xPos, yPos, 50, 50);
    println("X"   xPos   " Y:"  yPos);
  }

}
Ball ball1;
Ball ball2;
ArrayList<Ball> ballList = new ArrayList<Ball>();

void setup() {
  size(600, 600);
  ball1=new Ball(900, 900);
  ball2=new Ball(900, 900);
}

void draw() {
  
  background(150);
  if (mousePressed ) {
    ball1.xPos= mouseX;
    ball1.yPos=mouseY;
  }

  ball1.drawBall();
}

CodePudding user response:

The issue here are that you're not managing the array at all, and instead are working with the same object all the time. I re-wrote your example using the arraylist to store the Ball objects, and also to create new balls when the user clicks a mouse button.

Please note here that I'm deliberately using the "last item added to the arraylist" instead of creating an object which would be a reference to the this object. This is because I don't want to confuse you with references and such.

ArrayList<Ball> ballList = new ArrayList<Ball>();

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

void draw() {  
  background(150);
  
  // if the mouse button is held down, set the ball's coordinates to the mouse coordinates
  if (ballList.size() > 0 && mousePressed) {
    ballList.get(ballList.size() - 1).xPos = mouseX;  // 'ballList.get(ballList.size() - 1)' is the java way to get the last item added to an arrayList
    ballList.get(ballList.size() - 1).yPos = mouseY;
  }
  
  // draw every ball in the array (the weird format is just shorthand for a "for each" loop)
  for(Ball b : ballList) {
    b.drawBall();
  }  
}

// this method will trigger once every time the user press a mouse button
void mousePressed() {
  ballList.add(new Ball(mouseX, mouseY));
}

class Ball {
  float xPos, yPos; 
  
  Ball(float xPos, float yPos) {
    this.xPos= xPos;
    this.yPos= yPos;
  }
  
  void drawBall() {
    ellipse(xPos, yPos, 50, 50);
  }
}

By targeting the last created ball instead of a specific ball, you ensure that you're always using the right object. Also, notice how I just let the ball objects draw themselves in a loop. The 'for each' form ensure that whatever number of balls there currently is in the arrayList, they will all be printed anyway.

Good luck with this project and if something is confusing about my answer don't hesitate to reach out and ask questions about it.

Have fun!

  • Related