I'm trying to make a game with a cat that shoots lasers. The cat is supposed to shoot a short laser segment when the spacebar is pressed that is created again and again until it hits a wall, then stops. I haven't yet written the code to make the laser disappear after it's shot, but I think it should still work. The game functions fine until the spacebar is pressed, then it freezes. I assume that I'm causing some kind of infinite loop where the laser doesn't realize that it hit a wall and keeps on going forever, but I can't figure out what's causing it. Can someone please help?
LaserCat class:
Laser class:
import greenfoot.*;
/**
* Write a description of class LaserCat here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LaserCat extends Actor
{
private GreenfootImage Down1 = new GreenfootImage("KnightDog7.png");
private GreenfootImage Down2 = new GreenfootImage("KnightDog8.png");
private GreenfootImage Up1 = new GreenfootImage("KnightDog1.png");
private GreenfootImage Up2 = new GreenfootImage("KnightDog2.png");
private GreenfootImage Left1 = new GreenfootImage("KnightDog3.png");
private GreenfootImage Left2 = new GreenfootImage("KnightDog4.png");
private GreenfootImage Right1 = new GreenfootImage("KnightDog5.png");
private GreenfootImage Right2 = new GreenfootImage("KnightDog6.png");
private int animationSpeed = 10;
private int frame = 1;
private int direction;
private int timer = 0;
int catDirection = 2;
public void act()
{
checkKey();
checkFire();
}
private int speed = 1;
public void checkKey()
{
if (Greenfoot.isKeyDown("down"))
{
catDirection = 2;
goDown();
}
if (Greenfoot.isKeyDown("up"))
{
catDirection = 1;
goUp();
}
if (Greenfoot.isKeyDown("left"))
{
catDirection = 3;
goLeft();
}
if (Greenfoot.isKeyDown("right"))
{
catDirection = 4;
goRight();
}
}
public void goDown()
{
timer++;
setLocation(getX(),getY() + speed);
if (frame == 1 && timer % 5 == 0)
{
setImage(Down1);
frame = 2;
}
else if (frame == 2 && timer % 5 == 0)
{
setImage(Down2);
frame = 1;
}
}
public void goUp()
{
timer++;
setLocation(getX(),getY() - speed);
if (frame == 1 && timer % 5 == 0)
{
setImage(Up1);
frame = 2;
}
else if (frame == 2 && timer % 5 == 0)
{
setImage(Up2);
frame = 1;
}
}
public void goRight()
{
timer++;
setLocation(getX() + speed,getY());
if (Greenfoot.isKeyDown("down") == false && Greenfoot.isKeyDown("left") == false && Greenfoot.isKeyDown("up") == false)
{
if (frame == 1 && timer % 5 == 0)
{
setImage(Right1);
frame = 2;
}
else if (frame == 2 && timer % 5 == 0)
{
setImage(Right2);
frame = 1;
}
}
}
public void goLeft()
{
timer++;
setLocation(getX() - speed,getY());
if (Greenfoot.isKeyDown("down") == false && Greenfoot.isKeyDown("right") == false && Greenfoot.isKeyDown("up") == false)
{
if (frame == 1 && timer % 5 == 0)
{
setImage(Left1);
frame = 2;
}
else if (frame == 2 && timer % 5 == 0)
{
setImage(Left2);
frame = 1;
}
}
}
public void checkFire()
{
if(Greenfoot.getKey() == "space")
{
shootLaser();
}
}
public int getDirection()
{
return catDirection;
}
private int laserLength = 0;
public void shootLaser()
{
while (Laser.noWallDetected == true)
{
getWorld().addObject(new Laser(this), getX(), getY() + laserLength);
laserLength += 2;
}
laserLength = 0;
}
}
import greenfoot.*;
/**
* Write a description of class Laser here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Laser extends Actor
{
private LaserCat myCat;
private boolean checkedForWall = false;
public static boolean noWallDetected = true;
public Laser(LaserCat myCat)
{
this.myCat = myCat;
}
public void act()
{
if (checkedForWall == false)
{
if (wallDetected())
noWallDetected = false;
else
noWallDetected = true;
checkedForWall = true;
}
}
public boolean wallDetected()
{
Actor a = getOneIntersectingObject(Wall.class);
if(this.isAtEdge() || a != null)
return true;
else
return false;
}
}

