How do I decrease the amount of lag in my game? every time my player shoot, the game slows down SO MUCH. it gets about 10 frames and the game just goes to about a quarter of the original speed. Does anyone know how I can fix this?
import greenfoot.*;
public class Plane2 extends controllables
{
private boolean trueState = false;
private int speed;
private int timer = 10;
Bomb bomb = new Bomb();
public Plane2()
{
}
public void act()
{
checkKeys();
// adjusting the rotation
int dr = 0;
if (Greenfoot.isKeyDown("right")) dr++;
if (Greenfoot.isKeyDown("left")) dr--;
turn(dr*QVAL);
// adjusting the speed
int ds = 0;
if (Greenfoot.isKeyDown("up")) ds++;
if (Greenfoot.isKeyDown("down")) ds--;
speed += ds;
// limiting the speed
if (speed < 0) speed = 0;
if (speed > 10*QVAL) speed = 10*QVAL;
// moving
move(speed);
}
private void fire()
{
Bomb bomb = new Bomb();
if (Greenfoot.isKeyDown("space"))
getWorld().addObject(bomb, getX(), getY());
bomb.setRotation(getRotation());
}
private void checkKeys()
{
if (timer > 0) timer--;
if (timer == 0)
{
fire();
timer = 10;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bomb here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bomb extends SmoothMover
{
GifImage2 myGif2 = new GifImage2("explode.gif");
private int life = 22;
public Bomb()
{
}
public Bomb(Vector speed, int rotation)
{
super(speed);
setRotation(rotation);
addForce(new Vector(rotation, 15));
}
public void act()
{
Explosion explosion = new Explosion();
if (isAtEdge())
{
getWorld().addObject(explosion, getX(), getY());
getWorld().removeObject(this);
return;
}
else if(life <= 0)
{
getWorld().addObject(explosion, getX(), getY());
getWorld().removeObject(this);
return;
}
else if (isTouching(Boss.class))
{
getWorld().addObject(explosion, getX(), getY());
getWorld().removeObject(this);
return;
}
else
{
life--;
}
move(30);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Explosion here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Explosion extends Bomb
{
GifImage2 myGif2 = new GifImage2("explode.gif");
private int splodelife = 21;
/**
* Act - do whatever the Explosion wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setImage( myGif2.getCurrentImage() );
if (splodelife == 0)
{
Explosion explosion = new Explosion();
getWorld().removeObject(this);
}
else
{
splodelife--;
}
}
}
public void act()
{
if (isAtEdge() || life <= 0 || isTouching(Boss.class))
{
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(this);
return;
}
life--;
move(30);
}