Hello.
I have a counter, called Counter1, which is supposed to show the ammo of my character. Whenever my character moves over an ammocrate, the ammo is increased by 5, which i have already made the counter show. What i need to do is make it display the total ammo amount. So instead of starting at 0 and ijncreasing by 5 every time i pick up an ammocrate, it starts at 5, increases by 5 when i pick up a crate, and decreases by 1 every time i shoot ( use an ammo )
I have the folllowing code for the counter
I have the actor Ditzel, which is the character that removes the ammo crate and gains ammo.
notice the hitAnAmmo method and the ammo variable - is it possible for me to make the counter show whatever value that ammo variable has?
This is the code for my world, in which the counter is called.
Thanks very much :)
public class Counter1 extends Actor
{
private int totalCount =0;
public Counter1()
{
setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
}
/**
* Increase the total amount displayed on the counter, by a given amount.
*/
public void bumpCount(int amount)
{
totalCount += amount;
setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
}
public int getCount()
{
return totalCount;
}
}public class Ditzel extends Actor
{
public void act()
{
moveAndTurn();
shoot();
Pickup();
}
public void moveAndTurn()
{
if (Greenfoot.isKeyDown("left"))
{
setRotation(180);
if(getOneObjectInFront(Wall.class)==null)
move(3);
}
if (Greenfoot.isKeyDown("right"))
{
setRotation(0);
if(getOneObjectInFront(Wall.class)==null)
move(3);
}
if (Greenfoot.isKeyDown("up"))
{
setRotation(270);
if(getOneObjectInFront(Wall.class)==null)
move(3);
}
if (Greenfoot.isKeyDown("down"))
{
setRotation(90);
if(getOneObjectInFront(Wall.class)==null)
move(3);
}
}
int ammo = 5;
private boolean spaceDown;
public void shoot()
{
if(!spaceDown && Greenfoot.isKeyDown("space"))
{
if(Munition()==true)
{
spaceDown=true;
Bullet bullet = new Bullet();
bullet.setRotation(getRotation());
getWorld().addObject(bullet, getX(), getY());
ammo--;
}
}
if(spaceDown && !Greenfoot.isKeyDown("space"))
{
spaceDown=false;
}
}
public boolean Munition()
{
if(ammo>0)
{
return true;
}
else
{
return false;
}
}
public void Pickup()
{
Actor ammo1;
ammo1 = getOneObjectAtOffset(0,0, Ammo.class);
if (ammo1 != null)
{
hitAnAmmo();
World world;
world = getWorld();
world.removeObject(ammo1);
ammo=ammo+5;
}
}
private void hitAnAmmo()
{
Labyrinth LabWorld = (Labyrinth) getWorld();
Counter1 counter = LabWorld.getCounter1();
counter.bumpCount(5);
}
private Actor getOneObjectInFront(Class c)
{
GreenfootImage myImage = getImage();
int distanceToFront = myImage.getWidth();
int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
return (getOneObjectAtOffset(xOffset, yOffset, c));
}
}public class Labyrinth extends World
{
private Counter theCounter;
private Counter1 theCounter1;
/**
* Constructor for objects of class MyWorld.
*
*/
public Labyrinth()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(700, 600, 1);
populate();
prepare();
theCounter = new Counter();
addObject(theCounter, 650, 15);
theCounter1 = new Counter1();
addObject(theCounter1, 63, 15);
}
public Counter getCounter()
{
return theCounter;
}
public Counter1 getCounter1()
{
return theCounter1;
}
