After I fire bullets in this scenario it begins to freeze and lag. It seems to happen when I fire bullets quickly or hold down the fire button. But, sometimes it will happen from the first bullet. I've tried increasing the reload time to give the scenario more time to process but have had no success. I'm getting no error messages or anything. If I reset the scenario it will lag right from the beginning. If I close and reopen it will work until I try to fire again.
Code for firing actor
Code for bullet
public class Robo extends Actor
{
private int smileyPoints;
private int gunReloadTime; // The minimum delay between firing the gun.
private int reloadDelayCount;
/**
* Initialise this Robo.
*/
public Robo()
{
gunReloadTime = 20;
reloadDelayCount = 0;
}
/**
* Act - do whatever the Robo wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
//move(5);
lookForWorm();
checkDirectionKey();
checkKeyPress();
reloadDelayCount++;
}
//Action for when Robo finds Smiley
public void lookForWorm()
{
if ( isTouching (Smiley.class) )
{
removeTouching (Smiley.class);
Greenfoot.playSound("hooray.wav");
smileyPoints = smileyPoints + 1;
if ( smileyPoints == 5)
{
Greenfoot.stop();
Greenfoot.playSound("fanfare.wav");
}
}
}
//Check if directional key is pressed, if so react
public void checkDirectionKey()
//Turn Robo left
{
if (Greenfoot.isKeyDown("left"))
{
turn(-4);
}
//Turn Crab Right
if (Greenfoot.isKeyDown("Right"))
{
turn(4);
}
//Move Robo up
if (Greenfoot.isKeyDown("up"))
{
move(5);
}
//Move Robo down
if (Greenfoot.isKeyDown("down"))
{
move(-5);
}
}
//Check if key is press, if so then react
public void checkKeyPress()
{
//move up
if (Greenfoot.isKeyDown("W"))
{
setLocation(getX(), getY()-4);
}
//move down
if (Greenfoot.isKeyDown("S"))
{
setLocation(getX(), getY()+4);
}
//move left
if (Greenfoot.isKeyDown("A"))
{
move(-4);
}
//move right
if (Greenfoot.isKeyDown("D"))
{
move(4);
}
//Create bullet at 100, 100
if (Greenfoot.isKeyDown("L"))
{
fire();
}
}
//fire bullet if gun is ready
private void fire()
{
if ( reloadDelayCount >= gunReloadTime )
{
getWorld().addObject( new Bullet(), getX(), getY());
reloadDelayCount = 0; //time since last shot fired
}
}
}
/*Bullet will disappear when 0 life*/
private int life = 30;
/*Move bullet in direction it was created*/
public int getRotation;
public void act()
{
move();
hitSpider();
//noLife();
ifTouchingEdge();
}
public void move()
{
move(2);
/*if ( getRotation(Bullet.class) = 0)
{
move(2);
}**/
}
/* public void noLife()
{
if (life <= 0)
{
getWorld().removeObject(this);
}
} */
//Action for when bullet hits spider
public void hitSpider()
{
if ( isTouching (Grunt.class) )
{
removeTouching (Grunt.class);
Greenfoot.playSound("Squish.wav");
}
}
//When bullet reaches right edge, disappear
public void ifTouchingEdge()
{
if ( getX() == 599)
{
//life = 0;
getWorld().removeObject(this);
}
}
}
