We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
draw game (Read 461 times)
draw game
Feb 2nd, 2008, 6:56pm
 
void draw() {
 background(204);
 if(key=='1') {
   return;
 }
 line(0, height, width, 0);
}

I want to pass the key =='1' first time to disappear the line(0, height, width, 0);
and the second time pass the key appear the line, but all must in draw, can't use the Keyboard viod

Re: draw game
Reply #1 - Feb 2nd, 2008, 7:58pm
 
You want to toggle the line on and off. This is probably done best by using booleans, though I don't know how good you can handle them. Have a look at the following sketch.

The keyPressed signal will be passed every new draw cycle, for as long as the key is held down. But we only want to react to the first signal, therefore we only allow the first signal and then block the following ones using the 'keyreset' check. This "blockade" is opened as soon as the key is released.

Quote:


// visibility of line
boolean linevisible = false;

// prevent uncontrollable toggle
boolean keyreset = true;

void setup() {
 size(200,200);
}

void draw() {
 background(204);

 // IF a key is Pressed...
 // AND the key is the "1" key...
 // AND it's the first keyPressed signal
 if (keyPressed && key == '1' && keyreset == true) {
   // got first signal
   keyreset = false;

   // if the line was "not visible" set to "visible"
   if(linevisible == false) {
     linevisible = true;
   }
   // if the line was "visible" set to "not visible"
   else {
     linevisible = false;
   }

 }

 // draw the line only if set to "visible"
 if(linevisible == true) {
   line(0, height, width, 0);
 }

}

// check if the button has been released
void keyReleased() {
 keyreset = true;
}




cheers.
Re: draw game
Reply #2 - Feb 3rd, 2008, 2:41am
 
thank you,but I can only use the key =='1' and the other can't use
I mean :

// visibility of line
boolean linevisible = false;

// prevent uncontrollable toggle
boolean keyreset = true;

void setup() {
 size(200,200);
}

void draw() {
 background(204);  

 // IF a key is Pressed...
 // AND the key is the "1" key...
 // AND it's the first keyPressed signal
 if ( key == '1' && keyreset == true) {
   // got first signal
   keyreset = false;

   // if the line was "not visible" set to "visible"
   if(linevisible == false) {
     linevisible = true;  
   }  
   // if the line was "visible" set to "not visible"
   else {
     linevisible = false;  
   }

 }

 // draw the line only if set to "visible"
 if(linevisible == true) {
   line(0, height, width, 0);
 }

}
Re: draw game
Reply #3 - Feb 3rd, 2008, 5:29pm
 
Why can't you use keyPressed or keyReleased? Are you using them for another key check?
Page Index Toggle Pages: 1