Hi, I'm making a tank game where each tank only has 20 missiles to shoot. I have gotten the tanks to stop shooting after 20 shots, but I would like a counter to display this.
I guess I'll paste all the code so you can review it.
Tank:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Blue extends DoubleMove
{
public Blue()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 65, image.getHeight() - 50);
setImage(image);
ammo=20;
}
public Blue(AmmoBlue BlueShots)
{
}
private int ammo;
public void act()
{
//Controls
if (Greenfoot.isKeyDown("left"))
turn(-2);
if (Greenfoot.isKeyDown("right"))
turn(2);
if (Greenfoot.isKeyDown("up"))
move(2.5);
if (Greenfoot.isKeyDown("down"))
move(-1.5);
if (ammo > 0 && (Greenfoot.isKeyDown("enter") && System.currentTimeMillis()>FireDelay+250))
{
BlueMissile bluemissile = new BlueMissile();
getWorld().addObject(bluemissile, getX(), getY());
bluemissile.setRotation(getRotation());
ammo--;
FireDelay = System.currentTimeMillis();
}
// Slow on Touching the Sponge
if(isTouching(JellyWall.class))
move(-1);
}
private double FireDelay;
}
Counter:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class AmmoBlue extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
/**
* Create a new counter, initialised to 0.
*/
public AmmoBlue()
{
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
{
}
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
}
private double ValueDelay;
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
World:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Level1 extends World
{ public Level1()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1000, 500, 1);
prepare();
}
public void act()
{
setPaintOrder(Blue.class, BlueMissile.class);
}
private void prepare()
{
Clock clock = new Clock();
Red player = new Red();
addObject(player,178,74);
JellyWall jellywall = new JellyWall();
addObject(jellywall, 300,71);
JellyWall jellywall2 = new JellyWall();
addObject(jellywall2,300,384);
JellyWall jellywall4 = new JellyWall();
addObject(jellywall4,700,71);
JellyWall jellywall3 = new JellyWall();
addObject(jellywall3,700,378);
ScoreRed scorered = new ScoreRed();
addObject(scorered,199,19);
ScoreBlue scoreblue = new ScoreBlue();
addObject(scoreblue,917,18);
Clock timer = new Clock();
addObject(timer,491,18);
AmmoBlue ammoblue = new AmmoBlue();
addObject(new AmmoBlue(),809,18);
AmmoRed ammored = new AmmoRed();
addObject(ammored,96,20);
Blue blue = new Blue();
addObject(blue,803,390);
}
}
So I just need a counter that displays the current ammo for Blue, and I should be able to use this for Red later as well. I've looked at the tutorial and Michael did something in the world prepare such as this:
Blue blue = new Blue(AmmoBlue);
addObject(blue,803,390);
and then in Blue:
public Blue(AmmoBlue BlueShots)
{
}
but this says in the world prepare method 'cannot find variable AmmoBlue'
Please help!


