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
keyPressed (Read 1199 times)
keyPressed
Dec 14th, 2009, 10:10am
 
hi,

i'd like to know how to convert the following idea into a code:
if i press a key (on the keyboard) for example 'b' i'd like to have an action but this action should be repeated until i press another key or the same key again. so far i know how to create an action (ex.

if(keyPressed) {
       if (key == 'y' || key == 'Y') {
         newTarget4 ();

)

but i don't know how to repeat the action and how to stop it with keyboard.

thanks for helping
Re: keyPressed
Reply #1 - Dec 14th, 2009, 10:23am
 
You create a boolean variable and only do the action when the variable is true. Now you set one key to set the variable to false, and another to turn it on again.




boolean action = false;

void setup() {
 size(400,400 );

}

void draw() {
background(0);
fill(255,30);

if(action){
   fill(255,0,0);  
}
ellipse(200,200,100,100);
}

void keyReleased(){
if (key == 'q' || key == 'Q') {
       action = true;
   
}

if (key == 'W' || key == 'w') {
       action = false;
}
}
Re: keyPressed
Reply #2 - Dec 14th, 2009, 10:27am
 
this seems to be the right thing... thank you very much


Re: keyPressed
Reply #3 - Dec 14th, 2009, 12:00pm
 
hello again,

is there a possibility to create  this  without ''void draw()'' ?
because my code does not tolerate ''draw''. maybe there is a kind of loop or something like this.

here is a class out of my object orientated code. i hope somebody can help me.

class Essen {

 PVector position;
 PVector target;

 Essen (float theX, float theY, float theZ) {
   position = new PVector (theX, theY, theZ);
   target   = new PVector ();
   newTargetm ();
   newTarget1 ();
   newTarget2 ();
   newTarget3 ();
   newTarget4 ();
 }

 void move () {
   PVector step = new PVector ();
   step.set (position);
   step.sub (target);
   step.div (5);
   position.sub (step);

   if(keyPressed) {
     if (key == '2' || key == '"') {
       newTarget1 ();
     }
   }
   if(keyPressed) {
     if (key == 'w' || key == 'W') {
       newTarget2 ();
     }
   }
   if(keyPressed) {
     if (key == 's' || key == 'S') {
       newTarget3 ();
     }
   }
   if(keyPressed) {
     if (key == 'x' || key == 'X') {
       newTarget4 ();

     }
   }
   if (position.dist (target) < 3) {
     newTargetm ();
   }

 }

 void newTarget1 () {
   for (int a=0; a < u.length; a++) {
     target.x = (u[a].position.x);
     target.y = (u[a].position.y);
     target.z = (u[a].position.z);
   }
 }

 void newTarget2 () {
   for (int a=0; a < uA.length; a++) {
     target.x = (uA[a].position.x);
     target.y = (uA[a].position.y);
     target.z = (uA[a].position.z);
   }
 }

 void newTarget3 () {
   for (int a=0; a < uB.length; a++) {
     target.x = (uB[a].position.x);
     target.y = (uB[a].position.y);
     target.z = (uB[a].position.z);
   }
 }
 void newTarget4 () {
   for (int a=0; a < uF.length; a++) {
     target.x = (uF[a].position.x);
     target.y = (uF[a].position.y);
     target.z = (uF[a].position.z);
   }
 }
void newTargetm () {
    for (int a=0; a < u.length; a++) {
   u[a].move ();
   target.x = (u[a].position.x + uA[a].position.x + uB[a].position.x + uF[a].position.x) / 4;
   target.y = (u[a].position.y + uA[a].position.y + uB[a].position.y + uF[a].position.x) / 4;
   target.z = (u[a].position.x + uA[a].position.z + uB[a].position.z + uF[a].position.x) / 4;
 }
 }



}


thanks again
Re: keyPressed
Reply #4 - Dec 14th, 2009, 12:41pm
 
please show your whole code.
How can your code run without void draw() ?
void draw is actually needed to make interaktion/animation etc.
Thats the kind of loop you asked for.
Re: keyPressed
Reply #5 - Dec 14th, 2009, 12:57pm
 
hey
the code is to long to post. draw is in the program butt not in the classes.
Re: keyPressed
Reply #6 - Dec 14th, 2009, 1:07pm
 
great. thats what we need. so i actually dont know what is unclear about my code.
put what is in draw wherever you need it.
i dont know where    newTarget4 (); appears but leave it where it is and put a if(active) in front of it.
thats it.
Re: keyPressed
Reply #7 - Dec 14th, 2009, 1:19pm
 
is it possible to create more ''draw's()'' in different classes. the reason is that there are four targets() in five classes which i'd like to control by the keyboard
Re: keyPressed
Reply #8 - Dec 14th, 2009, 1:50pm
 
you can create different functions in different classes. they can also be named draw. but it doesnt matter as you need to call them somewhere. you normally do that in the main draw function.

but like i said. no matter where your  newTarget4 (); is.
just put "if(active)" in front of it and it should work. as long as active is true and you press the other button to turn it off.

if you want to controll more then one target, just create more booleans, active1,2,3,4 for example.
Re: keyPressed
Reply #9 - Dec 14th, 2009, 2:21pm
 
yea yea yea...it works thank you very very much...
Re: keyPressed
Reply #10 - Dec 14th, 2009, 2:44pm
 
and a last question. how do i manage that one key switch the action on and of?
Re: keyPressed
Reply #11 - Dec 14th, 2009, 3:03pm
 
instead of

active = true;

write

active = !active;

this toggles it on/off
Page Index Toggle Pages: 1