Reputation: 23
I am making a simple game where the user must click on a ball that is moving towards the bottom of the screen. the user must click on the ball before it hits the bottom of the screen,when the user clicks on the ball, the ball is moved upwards by a pre-set value.
the issue i am having is that the user is able to just keep the left mouse button pressed and the ball will constantly keep moving up.
This is my draw function which calls another function once the mouse is pressed.
void draw(){
background(bg);
update_ball();
if (mousePressed){
mouse_hit();
}
}
this is the mouse_hit function
void mouse_hit(){
if (mouseX <= (B1.x_loc+B1.size) && mouseX >= (B1.x_loc-B1.size) &&
mouseY <= (B1.y_loc+B1.size) && mouseY >= (B1.y_loc-B1.size) ){
B1.y_loc+=-25;
// file.play();
}
}
what i want is to setup a delay so that the user has to wait X amount of seconds before pressing the mouse again.
i attempted to do this by trying to use the Second() function in processing, but this function returns whatever X seconds is the clock for the pc and then it restarts from 0 once it hits 59.i was not able to use this to create any type of delay.
Upvotes: 2
Views: 2002
Reputation: 42176
Don't use the mousePressed
variable. Use the mousePressed()
function:
void draw(){
background(bg);
update_ball();
}
void mousePressed(){
if (mouseX <= (B1.x_loc+B1.size) && mouseX >= (B1.x_loc-B1.size) &&
mouseY <= (B1.y_loc+B1.size) && mouseY >= (B1.y_loc-B1.size) ){
B1.y_loc+=-25;
// file.play();
}
}
The mousePressed
variable will be true for as long as the mouse is pressed. The mousePressed()
function is only called once per click. You could also use the mouseClicked()
or mouseReleased()
functions to get more information about the click event.
More info on these functions is available in the Processing reference.
Upvotes: 1