I am working on a project for class and I am having trouble with my shooting code. The first issue is making a displayed counter for our bullets. We don't know how to do that. Next, we need to figure out how to set a starting value for the bulletCount. Right now we have it set to stop the scenario at -300 bullets shot, because our current starting value is zero. Also, we need a way to stop the character from shooting after he is out of bullets, rather than stopping the scenario.
Here is the code for our main class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class catMario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class catMario extends Actor
{
private int gunReloadTime;
private int reloadDelayCount;
private int direction;
private int speed;
private int bulletCount;
/**
*
* Act - do whatever the catMario wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
keyMovement();
bounceBack();
checkFire();
bulletCounter();
} // Add your action codif (Greenfoot.isKeyDown("w"))
public void keyMovement()
{
if (Greenfoot.isKeyDown("w"))
{
jumpUp(1);
}
if (Greenfoot.isKeyDown("s"))
{
jumpDown(1);
}
if (Greenfoot.isKeyDown("a"))
{
move(-10);
}
if (Greenfoot.isKeyDown("d"))
{
move(10);
}
if (Greenfoot.isKeyDown("space"))
{
checkFire();
}
}
public void jump(int distance, int direction)
{
for (int i = 0; i < distance; i++)
{
super.setLocation(super.getX( ), super.getY() + direction);
}
}
public void jumpUp(int distance)
{
this.jump(distance, -10);
}
public void jumpDown(int distance)
{
this.jump(distance,10);
}
public void checkFire()
{
if(Greenfoot.isKeyDown("space"))
{
getWorld().addObject(new Lemon(), getX(), getY());
bulletCount= bulletCount - 1;
}
}
public void bulletCounter()
{
if ( bulletCount == -300){
Greenfoot.stop();
}
}
Here is the code for our bullet class, which is currently called lemon.
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lemon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lemon extends Actor
{
/**
* Act - do whatever the Lemon wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(-20);
}
}
