I have a fairly lame simulation that works mostly, except that sometimes it isn't picking up that the laser beam is intersecting with its target. It's probably something I am doing wrong, but I can't see it. I've included the code for the laser beam.
import greenfoot.*;
import java.util.*; // For Lists
/**
* Write a description of class LaserBeam here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LaserBeam extends Actor
{
/**
* Constructs the LaserBeam, pointing in the right direction
*/
public LaserBeam(int direction)
{
setRotation(direction);
}
/**
* Act - do whatever the LaserBeam wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
move(10);
// Vanish if we reach the edge of the world
World choirWorld = getWorld();
if (getX() <= 0 || getY() <= 0 || getX() >= choirWorld.getWidth() || getY() >= choirWorld.getHeight())
{
List<LaserCat> cats = choirWorld.getObjects(LaserCat.class);
if (!cats.isEmpty())
{
LaserCat theCat = cats.get(0);
theCat.resetLaser();
}
choirWorld.removeObject(this);
}
else
{
// Check to see what we've hit
// If it's the squeaker, remove them
ChoirSinger choirSqueaker = (ChoirSinger)getOneIntersectingObject(ChoirSinger.class);
if (choirSqueaker != null && choirSqueaker.Squeak())
{
List<LaserCat> cats = choirWorld.getObjects(LaserCat.class);
if (!cats.isEmpty())
{
LaserCat theCat = cats.get(0);
theCat.resetLaser();
}
choirSqueaker.unblock();
choirWorld.removeObject(choirSqueaker);
choirWorld.removeObject(this);
}
}
}
}

