import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Gun here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Gun extends Actor
{
/**
* Act - do whatever the Gun wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.isKeyDown("t") )
{
turn(10);
}
if(Greenfoot.isKeyDown("space") )
{
getWorld().addObject(new Bullet(getRotation()), getX(), getY());
}
}
}
import greenfoot.*;
public class Bullet extends Actor
{
private int direction, speed;
public Bullet(int dir)
{
direction = dir;
speed = 15;
}
public void act()
{
setRotation(direction);
move(speed);
eat();
}
public void eat()
{
Actor adsasd;
adsasd = getOneObjectAtOffset(0 , 0 ,adsasd.class);
if (adsasd != null)
{
World world;
world = getWorld();
world.removeObject(adsasd);
}
}
}
The Gun class needs a field for the number of bullets it has to shoot. When shooting, the value should be decremented and then checked to see if any are left. If none left, remove gun from world.