so i had idea that i have 10 ammo which every i press space will fire and the ammo will minus. but if the ammo hit the enemy i will gain 1 ammo. so my fire code is in Spaceship class
and in my enemy class i try to increase the ammo. here my enemy code
can anyone help me so the ammo will increase after i hit the enemy.
Sorry for bad english.Thankyou...
public class Spaceship extends Actor
{
/**
* Act - do whatever the Spaceship wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int speed=5;
private boolean firerate = true;
public int ammo = 10;
public Spaceship()
{
GreenfootImage image = getImage();
int newHeight = (int)image.getHeight() / 4;
int newWidth = (int)image.getWidth()/ 4;
image.scale(newWidth, newHeight);
}
public void act()
{
// Add your action code here.
spaceshipMove();
}
public void spaceshipMove()
{
if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("A"))
{
setLocation(getX()-speed, getY());
}
if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("D") || Greenfoot.isKeyDown("right"))
{
setLocation(getX()+speed, getY());
}
}
public void fireTheBullet()
{
if(ammo == 0)
{
getWorld().showText("GameOver", 500, 500);
Greenfoot.stop();
}
if(Greenfoot.isKeyDown("space") && firerate == true)
{
getWorld().addObject(new Bullet_1(),getX(), getY() - 55);
firerate = false;
ammo--;
}
else if(!Greenfoot.isKeyDown("space") )firerate = true;
}
}
}public class Enemy extends Actor
{
/**
* Act - do whatever the Enemy wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public int speed = 2;
public boolean tambah = false;
public void act()
{
// Add your action code here.
enemyMove();
}
public void enemyMove()
{
setLocation(getX(), getY()+speed);
}
public void removeAtEdge()
{
//if(getY() == 699) getWorld().removeObject(this);
}
public void bulletHit()
{
Actor bullet = getOneIntersectingObject(Bullet.class);
if(getY() == 699) getWorld().removeObject(this);
else{
if(bullet != null)
{
Spaceship addbullet = new Spaceship();
getWorld().removeObject(bullet);
addbullet.ammo++;
getWorld().removeObject(this);
tambah = true;
}
}
}
}
