Loading...
Logo
Processing Forum
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? :)

Replies(11)


like this


Copy code
  1.   boolean klick () {
  2.     
  3.    if (darueber () && mousePressed) {
  4.      if (onoff == "off") {
  5.      onoff = "on";
  6.      }
  7.      else
  8.      {
  9.      onoff = "off";
  10.      xPos = random (0, 400);
  11.      yPos = random (0, 400);
  12.      }
  13.      return true;
  14.    }
  15.    else
  16.    {
  17.      return false;
  18.    }
  19.   }

Oh how great. :)


just out of curiosity: would it be possible that the object disappears with a click and reappears on a random spot without the second click? Do you know a code for that too? 

I'm a newbee so it would be great if somebody helped me on that too. :)


then leave onoff off so it paints all the time


Copy code
  1. boolean klick () {
  2.     
  3.    if (darueber () && mousePressed) {
  4.  
  5.      xPos = random (0, 400);
  6.      yPos = random (0, 400);

  7.    } // if 

  8. }  // method




Thank you. :)

One last question:
Now I have to count the klicks on the project. 
I have this so far, but it counts not only clicks on the object but all clicks, even if i just click on the background.

This is my code so far, what can I do?

FIRST PAGE:

Button knopf;
int klick = 0;

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 ();
  klick++;
  println (klick);
}

CLASS PAGE:

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") {
     xPos = random (0, 400);
     yPos = random (0, 400);
     
     }
     else
     {
     onoff = "off";
     xPos = random (0, 400);
     yPos = random (0, 400);
     }
     return true;
   }
   else
   {
     return false;
   }
  }
 }




Do the klick++;
Only in klick() in the class
after the if darueber....


Why do you paint it when onoff is off?
Isn't on clearer?

Dont' know where to do the klick++

here's my code, what did I do wrong?

class page:

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 ( int showk = 0;) {
    
   if (darueber () && mousePressed) {
     if (onoff == "off") {
     xPos = random (0, 400);
     yPos = random (0, 400);
     }
     else
     {
     onoff = "off";
     xPos = random (0, 400);
     yPos = random (0, 400);
     }
     return true;
   }
   else
   {
     return false;
   }
 
  showk++;
  println (showk);}
   
  }

first page: 

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 ();
}

Sorry, on a journey...

Berlin, germany

Do klick++
after if.... darüber.... 

In func boolean klick...
Before the 2nd click




________________________ ,

Before the 2nd if of course

maybe I'm too dumb, that doesn't work. :(
Do you have any idea what I've done wrong?

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)

  {
    int showk = 1; 
   showk=showk+1;
   println (showk);
   
     if (onoff == "off") {
     xPos = random (0, 400);
     yPos = random (0, 400);
     }
     else
     {
     onoff = "off";
     xPos = random (0, 400);
     yPos = random (0, 400);
     }
     return true;
  }
   else
   {
     return false;
   }
 
  }
   
  }

Wait...

int showk = 1; 

before setup() please
Otherwise he starts at 1 always...