This site requires JavaScript, please enable it in your browser!
Greenfoot back
footpickle
footpickle wrote ...

2020/3/2

My game is VERY laggy

footpickle footpickle

2020/3/2

#
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?
footpickle footpickle

2020/3/3

#
Can anyone help me? my game lags like Minecraft shaders on a pc with 4 gigs of ram
danpost danpost

2020/3/3

#
Show player codes and codes of what is being shot.
footpickle footpickle

2020/3/3

#
Player code:
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;
     }
        
    }
      
        
  }
    
Bomb code:
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);
        
     }
}
and here is the explosion that occurs after the bomb:
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--;
        }
    }    
}
danpost danpost

2020/3/3

#
Change act method in Bomb class to:
public void act()
{
    if (isAtEdge() || life <= 0 || isTouching(Boss.class))
    {
        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(this);
        return;
    }
    life--;
    move(30);
}
footpickle footpickle

2020/3/3

#
Thank you so much, my game is nowhere near as laggy as before!
danpost danpost

2020/3/3

#
footpickle wrote...
Thank you so much, my game is nowhere near as laggy as before!
The main issue fixed there was your line 32 in the Bomb class act method where you were unnecessarily unconditionally creating a new Explosion object each and every act step.
footpickle footpickle

2020/3/3

#
Oh, so it was making TONNS of unnecessary objects? From my experience, more actors in the world cause more lag because the program can't keep up with the amount of data all the objects are sending? or something like that, I guess...
danpost danpost

2020/3/3

#
footpickle wrote...
Oh, so it was making TONNS of unnecessary objects? From my experience, more actors in the world cause more lag because the program can't keep up with the amount of data all the objects are sending? or something like that, I guess...
Well, yes -- anytime you have more than 100 or so actors doing stuff in the world, lag will creep in (due to CPU processing time). However, image processing is a hog on CPU time as well -- and multiple images are created for each Explosion object created.
You need to login to post a reply.