Hello, I have created a game where the main object (in this case, a ship) is shooting at enemy objects. The ship has a limited amount of bullets, and once the bullets finish, it must acquire the Ammo object that spawns once the ammo = 0. I'm having trouble making the object spawn once randomly in the world, as it spawns multiple times. I also need to know how to reload the bullets back to the original amount.
void myMethod()
{
if (!allowedToFire && Greenfoot.isKeyDown("space"))
{
if (munition() == true)
{
allowedToFire = true;
Greenfoot.playSound("shootsound.wav");
getWorld().addObject(new Bullet(direction), getX(), getY());
--ammo;
}
}
if (allowedToFire && !Greenfoot.isKeyDown("space"))
{
allowedToFire = false;
}
}
public boolean munition(){
if(ammo>0){
return true;
}
else{
return false;
}
}
private void spawnAmmo()
{
if (ammo == 0)
{
((OceanWorld)getWorld()).spawnAmmo();
}
}
}
