Tiwaz
YaBB Newbies
Offline
Posts: 3
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