object which dissappears and reappears on a random spot
in
Programming Questions
•
10 months ago
hey guys,
I need help.
I would like to have an object which disappears and reappears on a random spot.
I can make it dissapear with one click but it reappears on the same spot with another click.
So can somebody tell me what I could look for, I don't find anything that could help me on the reference sheet.
Here is my code that I already have:
Button knopf;
void setup () {
size (400, 400);
knopf = new Button ( random (0, 400), random (0, 400), 50, 50);
}
void draw () {
background (0);
knopf.zeichne () ;
}
void mousePressed () {
knopf.klick ();
println (knopf.onoff);
}
and this is the class:
class Button {
float xPos;
float yPos;
int weite;
int hoehe;
String onoff = "off";
Button (float xPos, float yPos, int weite, int hoehe){
this.xPos = xPos;
this.yPos = yPos;
this.weite = weite;
this.hoehe = hoehe;
}
void zeichne () {
if (onoff == "on") {
}
else {
rect (xPos, yPos, weite, hoehe);
}
stroke (255, 0, 0);
strokeWeight (5);
}
boolean darueber () {
if (mouseX > xPos
&&mouseX < xPos+weite
&&mouseY > yPos
&& mouseY < yPos+hoehe)
{
return true;
}
else
{
return false;
}
}
boolean klick () {
if (darueber () && mousePressed) {
if (onoff == "off") onoff = "on";
else onoff = "off";
return true;
}
else
{
return false;
}
}
}
Could somebody please help me? :)
1