I'm trying to code a racing game and need help with the booster-system. I implemented two cars with two separate bars. The bars should gradually decrease while the cars get faster, but I can't seem to have these two operations happen simultaneously. When I press the button to use the booster, the car stops. I'm fairly new to Greenfoot, so I have no idea what the problem might be. Is there a way to solve it?
Here's the code from the car/player1 class:
Here's the code from booster:
And here the one from the subclass booster1:
Thanks a lot in advance
import greenfoot.*; public class player1 extends player { int speed; public static boolean canUseBooster1 = true; public player1(){ speed = 4; } public void act() { checkKeyPress("a","d","w","s", "f"); checkBooster1(); } public void checkKeyPress(String left, String right, String up, String down, String f) { if (Greenfoot.isKeyDown(right)) { turn(speed); } if (Greenfoot.isKeyDown(left)) { turn(-speed); } if (Greenfoot.isKeyDown(up)) { move(speed); } if (Greenfoot.isKeyDown(down)) { move(-speed); } if(Greenfoot.isKeyDown(f) && canUseBooster1) { speed = 10; } else{ speed = 4; } } public void checkBooster1(){ if (1==1){ //set it to 1=1, so I can test it canUseBooster1 = true; } else{ canUseBooster1 = false; } } }
import greenfoot.*; public class booster extends Actor { int fuel = 100; int fuelBarWidth = 20; int fuelBarHeight = 100; int pixels = (int)fuelBarHeight/fuel; public booster(){ update(); } public void act() { update(); } public void update(){ setImage(new GreenfootImage(fuelBarWidth+2, fuelBarHeight+2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.WHITE); myImage.drawRect(0,0, fuelBarWidth+1, fuelBarHeight+1); myImage.setColor(Color.BLUE); myImage.fillRect(1,1, fuelBarWidth, fuel*pixels); } public void loseFuel(int x){ fuel = fuel-x; update(); } }
import greenfoot.*; public class booster1 extends booster { long timer = 100000; public void act() { update(); checkKeyPress("f"); } public void checkKeyPress(String f){ while(Greenfoot.isKeyDown(f)){ if(timer > 0){ timer--; if(timer == 0){ loseFuel(1); update(); timer = 100000; } } } } }