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 1000 times)
keyPressed()?
Jul 22nd, 2009, 7:50pm
 
hi there,

I'm trying to set up a simple game where players interact with keys, holding them to move up & down.

First thing, I've noticed, when switching between up or down, there's a bit of a lag... and you can't hold the key (say, hold down constantly, then press up when you want to move up).. It happens when switching between up and down, presumably caused by the holding of the previous key. Any way round this? A 'keyHeld' kind of function?

More importantly, it seems two players can't interact with the same keyboard at once. That is, Processing will only take the input from one key at a time... Anyway around this?

Regards,

Matt

e.g

 if((keyPressed == true) && (key == CODED)) { // controls for arrow key
   if (keyCode == UP) {
     pad1 = pad1 - 7;
   }
   else if (keyCode == DOWN) {
     pad1 = pad1 + 7;
   }
   if(pad1 > height) {
     pad1 = height;
   }
   else {
     if (pad1 <= -1) {
       pad1 = 0;
     }
   }
 }
     
       if(keyPressed == true) { //
   if (key == 'w') {
     pad2 = pad2 - 7;
   }
   else if (key == 's') {
     pad2 = pad2 + 7;
   }
       }
       
   if(pad2 > height) {
     pad2 = height;
   }
   else {
     if (pad2 <= -1) {
       pad2 = 0;
     }
     
Re: keyPressed()?
Reply #1 - Jul 22nd, 2009, 9:24pm
 
One other q, when I throw in PFont for the scores... it winds up all slow...

Delete the PFont code blocks and it all runs smooth again... *shrugs*

float pad1 = 0; //player 1 bat y pos
float pad2 = 0; //player 2 bat y pos
int d = 30; //ball diameter

float x1 = 400; //ball x pos
float x2 = 200;//ball y pos
float direction = +0.0;
float direction2 = -0.0;
float h = 40.0; //bat diff
int s1 = 0; //p1 score
int s2 = 0; //p2 score

void setup() {
 size(800, 400);
 frameRate(100);


}

void draw() {
 smooth();
 background(50);
 strokeWeight(6);
 stroke(255);
 line(400, 0, 400, height); // playboard net/line

 PFont score1;
 score1 = loadFont("Perpetua-Bold-48.vlw");
 textFont(score1, 100);
 text(s1, 175, 100);

 PFont score2;
 score2 = loadFont("Perpetua-Bold-48.vlw");
 textFont(score2, 100);
 text(s2, 600, 100);


 x1 = (x1 + direction);  // ball movement

 x2 = (x2 - direction2);

 if(x2 > height) {
   direction2 = +5.0;
 }

 if (x2 < 0 && direction2 == +5.0) {
   direction2 = -5.0;
 }
 if (x2 < pad1+h && x2 > pad1-h && x1 > width-10) {
   direction = -5.0;
 }

 if (x2 < mouseY+h && x2 > mouseY-h && x1 < 1) {
   direction = +5.0;
 }


 if (x1 < -10) {
   x1 = 400;
   x2 = 200;
   direction = +0.0;
   direction2 = -0.0;
   s2 = s2 + 1;
 }

 if (x1 > width+10) {
   x1 = 400;
   x2 = 200;
   direction = +0.0;
   direction2 = -0.0;
   s1 = s1 + 1;

 }

 strokeWeight(6);              //ball
 stroke(70, 80, 90, 255);
 ellipse(x1, x2, d, d);

 strokeWeight(1);              
 rectMode(CENTER);
 rect(width, pad1, 20, 80); // player 2 bat
 rect(0, mouseY, 20, 80);      // player 1 bat





 if((keyPressed == true) && (key == CODED)) { // controls for arrow key
   if (keyCode == UP) {
     pad1 = pad1 - 7;
   }
   else if (keyCode == DOWN) {
     pad1 = pad1 + 7;
   }
   if(pad1 > height) {
     pad1 = height;
   }
   else {
     if (pad1 <= -1) {
       pad1 = 0;
     }
   }
 }
}


void mouseClicked() { //click starts ball
 direction = +5.0;
 direction2 = -4.0;
}

void keyPressed() {

 if(key == ' ') { //spacebar starts ball
   direction = +5.0;
   direction2 = -4.0;
 }
}




Re: keyPressed()?
Reply #2 - Jul 22nd, 2009, 10:52pm
 
Probably want to use booleans to remember if a button has been pressed.
Code:

void draw() {
...
 if (aFlag)
   text("a is pressed", 20,20);
 else
   text("a was released", 20,20);
...
}

void keyPressed() {
 if (key == 'a')
   aFlag = true;
}

void keyReleased() {
 if (key == 'a')
   aFlag = false;
}

The keyPressed() function is called as often as your keyboard repeat rate (how fast it adds the same letter if you're holding down a key in a text box).

Edit:
Initialize the font inside setup() only.
Doing so in draw() will reload the font every frame and only as fast as it can read from your hard drive.
Re: keyPressed()?
Reply #3 - Jul 23rd, 2009, 12:56am
 
Thank you.
Re: keyPressed()?
Reply #4 - Jul 23rd, 2009, 1:15am
 
This has cropped up before.  Booleans do work well to resolve the delay issue and allow for multiple keypresses.
Page Index Toggle Pages: 1