I want to make a laser limit in this scenario, but I can't figure out how to get it to work. Here is the code for the laser limit and the rocket.
Relevant Rocket Code:
LaserLimit Code:
The entire code works, I just can't seem to get the lasers to stop firing once the counter reaches 0. I have tried a variety of methods to try and get it to work. Thanks in advance to anyone who can help me!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | public class Rocket extends Actor { //Variables used later private int speed = 3 ; public boolean shooting = false ; LaserLimit limit = new LaserLimit(); private int lasers = limit.lasers; public void act() { checkKeys(); cheats(); if (cheatEnabled == true ) { rapidFire(); } if (cheatEnabled == false ) { if (lasers > 0 ) { shootWithSpace(); } if (lasers == 0 ) { if (Greenfoot.isKeyDown( "space" ) && !shooting) { shooting = true ; } if (!Greenfoot.isKeyDown( "space" )) { shooting = true ; } } } } private void shootWithSpace() { //If the spacebar is pressed, a laser is fired if (Greenfoot.isKeyDown( "space" ) && !shooting) { getWorld().addObject( new Laser(), getX(), getY()); Greenfoot.playSound( "pew.mp3" ); Galaxy world = (Galaxy) getWorld(); world.updateLimit(); shooting = true ; } //If the space bar isn't pressed, reset the shooting variable to false if (!Greenfoot.isKeyDown( "space" )) { shooting = false ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public class LaserLimit extends Actor { //Variables used later public int lasers = 30 ; public LaserLimit() { //creates the image of the laser counter GreenfootImage img = new GreenfootImage( 100 , 30 ); img.setColor(Color.WHITE); img.drawString( "Lasers Left: " + lasers, 5 , 25 ); setImage(img); } public void lasersLeft() { lasers = lasers - 1 ; //Recreates the image of the laser counter if there are still lasers GreenfootImage img = getImage(); img.clear(); img.setColor(Color.WHITE); img.drawString( "Lasers Left: " + lasers, 5 , 25 ); } public void rapidFire() { //Recreates the image of the laser counter to infinity once the rapid fire cheat is enabled GreenfootImage img = getImage(); img.clear(); img.setColor(Color.WHITE); img.drawString( "Lasers Left: ∞" , 5 , 25 ); } } |