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.
IndexProgramming Questions & HelpPrograms › Help with a key-initiated movement...!
Page Index Toggle Pages: 1
Help with a key-initiated movement...! (Read 326 times)
Help with a key-initiated movement...!
Feb 4th, 2009, 5:26am
 
So here's my code:

int x = 50;
int y = 400;
int shotx = x+5;
int shoty = 0;
boolean firing = false;

void setup(){
 size(500,500);
 background(0);
 frameRate(30);
 smooth();
 stroke(#44D827);
 drawTank(x, y);
}

void draw(){
 background(0);
 drawTank(x,y);
 firing = false;
 if(shoty < 0){
   firing = true;
 }
 if(firing == true){
   point(shotx,shoty);
   shoty += 1;
 }    
}

void keyPressed(){ //results of pressing a key!
 if(keyCode == UP){
   moveTank(x,y,'U');
   y = y - 3;
 }
 if(keyCode == DOWN){
   moveTank(x,y,'D');
   y = y + 3;
 }
 if(keyCode == RIGHT){
   moveTank(x,y,'R');
   x = x + 3;
 }
 if(keyCode == LEFT){
   moveTank(x,y,'L');
   x = x - 3;
 }
 if(key == ' '){
   shoty = y+4;
 }
}

void drawTank(int x, int y){
 line(x,y,x,y+10);
 line(x,y,x+10,y);
 line(x+10,y,x+10,y+10);
 line(x,y+10,x+10,y+10);
 line(x-1,y-1,x-1,y+2);
 line(x+11,y-1,x+11,y+2);
 line(x-1,y+8,x-1,y+11);
 line(x+11,y+8,x+11,y+11);
 line(x+5,y+4,x+5,y-4);
}

void eraseTank(int x, int y){
 stroke(0);
 line(x,y,x,y+10);
 line(x,y,x+10,y);
 line(x+10,y,x+10,y+10);
 line(x,y+10,x+10,y+10);
 line(x-1,y-1,x-1,y+2);
 line(x+11,y-1,x+11,y+2);
 line(x-1,y+8,x-1,y+11);
 line(x+11,y+8,x+11,y+11);
 line(x+5,y+4,x+5,y-4);
 stroke(#44D827);
}

void moveTank(int x, int y, char z){ //char has to be U,D,R,L
 eraseTank(x,y);
 if(z == 'U'){
   drawTank(x, y-3);
 }
 if(z == 'D'){
   drawTank(x, y+3);
 }
 if(z == 'L'){
   drawTank(x-3, y);
 }
 if(z == 'R'){
   drawTank(x+3, y);
 }
}


Basically I'm trying to get the little tank image to shoot a bullet upwards when the spacebar is pressed but it's not working.  I hope this is an easy fix... not too sure why it isn't working.

Thanks!

-T
Re: Help with a key-initiated movement...!
Reply #1 - Feb 4th, 2009, 10:39am
 
Quote:
void draw(){
 background(0);
 drawTank(x,y);
 firing = false;
 if(shoty < 0){
   firing = true;
 }
 if(firing == true){
   point(shotx,shoty);
   shoty += 1;
 }    
}

The boolean is redundant and gets in the way: you shot, shoty becomes positive, firing is never true, you never see the point.

There are several issue with the code, like unnecessary translation of keyCode, not using else if (minor issue indeed), etc.
The drawTank and eraseTank could use a common function, avoiding code replication. But more importantly, you don't need eraseTank, as background() call takes care of erasing everything. Beside, due to anti-aliasing, this method of erasing is prone to leave ghost traces.

Re: Help with a key-initiated movement...!
Reply #2 - Feb 4th, 2009, 3:24pm
 
Thanks! I'm a doofus.  Just got confused by the y axis being inverted.  

Do you mind explaining the keyCode thing in more depth? I'm not too sure on that one.  

Again, Thanks.

-T
Re: Help with a key-initiated movement...!
Reply #3 - Feb 4th, 2009, 4:36pm
 
I meant you can pass directly the keyCode to moveTank() instead of a special one-letter code.

But actually, you don't need this routine at all, just update x or y like you do, and let the next draw() call do the drawing.
Page Index Toggle Pages: 1