ok. I've figured out how to make a ball stop, but why it keeps bouncing even when it stops ?
by the way, don't pay attention on ballImage thing, I was just playing around, trying to import an image in my code...
Quote:int width = 500;
int height = 300;
int buttonx = 400;
int buttony = 270;
int buttonwidth = 150;
int buttonheight = 30;
int count = 0;
int num = 0;
int sqrsize = 20;
int posx = 0;
float ballX, ballY, xVel, yVel, gravity = 0.1, friction = 0.99, ballDiam = 30;
PImage ballImage;
void setup(){
size(width, height);
textFont (createFont ("Georgia", 24));
strokeWeight(2);
ballImage = loadImage ("soccer-ball-1.png");
ballImage.resize (50,0);
ballX = width - ballImage.width;
ballY = height - ballImage.height;
}
void draw(){
background(255);
rect (buttonx, buttony, buttonwidth, buttonheight);
fill (0);
text (count, 450, 30);
if (mousePressed){
// if the mouse is held down, the ball location
// should be the same as the mouse location.
ballX = mouseX - ballImage.width / 2;
ballY = mouseY - ballImage.height / 2;
}
else{
// otherwise it should be traveling
// according to its velocity.
ballX += xVel;
ballY += yVel;
}
// draw the ball:
fill (0);
image (ballImage, ballX, ballY);
// here is the bounce part: if the ball gets close to
// any edge of the window, reverse its X or Y velocity.
// in this case, "too close" is "past the ball's radius."
if ((ballX < ballDiam / 2) || (ballX > width - ballDiam / 2)) xVel = -xVel;
if ((ballY < ballDiam / 2) || (ballY > height - ballDiam / 2)) yVel = -yVel;
else {
yVel += gravity;
xVel *= friction;
yVel *= friction;
if ((ballX >= buttonx) && (ballX < buttonx + buttonwidth) &&
(ballY >= buttony) && (ballY < buttony + buttonheight)) {
count = count + 1;
} else {
num = 0;
while (num < count) {
rect (posx, sqrsize * num, sqrsize, sqrsize);
num = num + 1;
}
posx = posx + sqrsize;
}
}
}
void mouseReleased(){
// when the mouse is released, "throw" the ball
// by setting its x and y velocities.
xVel = mouseX - pmouseX;
yVel = mouseY - pmouseY;
}