Hello,
I would like to have my game not to spawn bullets if the ammoCount variable = 0;
This is what I have in my Car Class:
I have thought to use an else if in the fire method, but unsure how to structure it.
Thanks in advance.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Mclaren here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mclaren extends Characters
{
/**
* Act - do whatever the Car wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
// instance Variables
private int damage;
private int gunDelay;
private int minGunDelay;
private int ammoCount = 5;
public Mclaren() // sets damage of the car to 0 also sets gunDelay and minGunDelay. Prob should have made minGunDelay Static in future.
{
damage = 0;
gunDelay = 0;
minGunDelay = 20;
}
public void act() // main part of the car that keeps running.
{
collectAmmo();
checkKeyPress();
checkCollision();
}
public void checkCollision() // increases damage of car when it hits a Vehicle.
{
Actor actor = getOneIntersectingObject(Mclaren.class);
if (actor != null)
{
damage++;
Greenfoot.playSound("crash.wav");
}
if (damage >= 100)
{
Greenfoot.stop();
}
}
public void moveLeft() // moves car left but stops it before it runs off road.
{
if(getX() <= 410)
{
setLocation(getX() - 2, getY());
}
}
public void moveRight() // moves the car right but stops it before it runs off road.
{
if(getX() < 400)
{
setLocation(getX() + 2, getY());
}
}
public void moveUp() // moves car up the screen
{
setLocation(getX(), getY() - 2);
}
public void moveDown() // moves car down the screen.
{
setLocation(getX(), getY() + 2);
}
public void collectAmmo(){
if(canSee(Ammo.class)){
collectItem(Ammo.class);
ammoCount ++;
}
}
public void fire()
{
if(gunDelay >= minGunDelay) // this lets u fire the gun and sets gun dealy to 0 so it has some interuption.
{
Rocket b = new Rocket();
getWorld().addObject(b, getX(), getY());
b.move(0);
gunDelay = -50;
ammoCount --;
}
else if (ammoCount == 0) {
}
}
public void checkKeyPress()
{
if(Greenfoot.isKeyDown("left")) // move car left
{
moveLeft();
}
if(Greenfoot.isKeyDown("right")) // move car right
{
moveRight();
}
if(Greenfoot.isKeyDown("up")) // Move car up
{
moveUp();
}
if(Greenfoot.isKeyDown("down")) // Move car down
{
moveDown();
}
if(Greenfoot.isKeyDown("space")) //this is my shoot key
{
fire();
}
// work in progress i want the car to change and start playing music. it has a problem where it will keep playing the music need some sort of delay.
String key = Greenfoot.getKey();
if(key != null)
{
if(key.equalsIgnoreCase("e"))
{
Greenfoot.playSound("spyhunter.wav");
}
}
gunDelay++; //added this for shooting
}
}
