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!
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; } }
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); } }