Draw and MouseClicked
in
Programming Questions
•
1 year ago
I'm having a little bit of trouble getting the mouseClicked method to loop. I'm trying to get an animation that displays a ball falling through the air due to gravity. Wherever the mouse is clicked the ball should appear and then fall from normal there. Unfortunately, the mouseClicked just starts the animation, but only continues to increment the ballY and deltaY if the mouse is clicked subsequently rather than automatically starting an animation. Because ballY is reset with every click, the result leaves nothing happening, and instead simply places the ball to the location of the click. Can anyone help? Here's the code:
final int BALL_SIZE = 25;
final float GRAVITY = 0.20; // fall acceleration
int width = 200, height = width * 2;
float ballX, ballY; // ball location
float deltaX, deltaY; // ball velocity
void setup() {
size(width, height);
smooth();
// Start the ball in the upper left.
ballX = 100;
ballY = 0;
// Set the rate of change for x and y.
deltaX = 5;
deltaY = 0;
background(255);
}
void draw() {
}
void mouseClicked() {
ballX = mouseX;
ballY = mouseY;
background(255);
// Draw the ball at the current location.
fill(80, 130, 190);
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
// Move the ball to the right and down.
ballY += deltaY;
deltaY += GRAVITY;
}
1