Mouse over box if released problem! help?
in
Programming Questions
•
5 months ago
hey folks! after reworking my code i stumbled upon a new issue. i used booleans for moving an object in and out. now i wanted to add the function that the object only moves out if the mouse is over the object and clicked. it almost works, but it seems like it is enough to just have moved the mouse once over the object to be able to move it out by clicking anywhere on the screen afterwards.
how can i tell the code more precisly that the mouse needs to be over the object (boolean=overBox;) when clicking to move it out?
here is my code!
how can i tell the code more precisly that the mouse needs to be over the object (boolean=overBox;) when clicking to move it out?
here is my code!
- int l2;
int w2;
int current_position_x1b;
int target_position_x1b;
float shake;
int speed = 10;
boolean movingIn = true;
boolean movingOut = false;
boolean overBox = false;
boolean locked = false;
void setup() {
size(900, 600);
l2 = 80;
w2 = 500;
rectMode(CENTER);
current_position_x1b = width+w2;
target_position_x1b = width/2+80;
shake=0;
}
void draw() {
background(175, 204, 231);
noStroke();
// Stripe One Yellow
pushMatrix();
//define movingOut
if(movingIn){
current_position_x1b -= speed;
}
//tell postion when to stop ny target_position
if(current_position_x1b <= target_position_x1b){
movingIn=false;
}
//define movingOut
if(movingOut){
current_position_x1b -= speed;
}
//translate the system to center and height
translate(current_position_x1b, height/6);
// Second (to respect translation!): Update shake, if:
if (movingIn==false && movingOut==false) {
//define area of the stripe for MouseOver
if ( mouseX > current_position_x1b - w2/2 &&
mouseX < current_position_x1b + w2/2 &&
mouseY > height/6 - l2/2 && mouseY < height/6 + l2/2 ) {
//then overBox ist true and shake is updated
overBox = true;
shake = PI*random(width/2 - 1, width/2 + 1) / width * 4;
//if stay there shake
if(!locked) {
rotate(shake);
} else { //else false..
overBox = false;
}
}
}
//draw stripe
fill(255, 234, 0);
rect(0, 0, w2, l2);
popMatrix();
}
void mouseReleased(){
//if mouse is clicked and over the box is true, move it out!
if(movingIn==false && overBox==true){
movingOut = true;
shake=0;
}
}
1