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

2021/4/6

Game lagging when touching a certain object

Gbasire Gbasire

2021/4/6

#
So I've made an update on my game, (making the text animate itself character by character, and some minor changes), and the game started to lag a lot more when touching "TallGrass.class". The worlds also seem to take some time to load (about 0.5 sec before update, about 2-3 sec after update). part of the code of Boy.class when he touches grass :
public void checkGrass()
    {
        if(isTouching(TallGrass.class))
        {
            if(Greenfoot.getRandomNumber(200)<1)
            {
                getWorld().addObject(new TransitionIn(), 300, 200);
                previousworld = "ROUTE";
                speed = 0;
            }
            if(Greenfoot.getRandomNumber(5)+1<2)
                getWorld().addObject(new Dust(),getX(),getY());
        }
    }
TransitionIn starts a new WorldWildBattle world, where the player can battle with his pokémon against a wild pokémon. the way the caracters are animated is this one :
public void animateText(String animation)
    {
        char[] letters = animation.toCharArray();
        int length = animation.length();
        if(texti < length)
        {
            writing += letters[texti];
            texti++;
        }
        getWorld().showText(writing, getX(), getY());
    }
One other change I've done is that I've deleted all unnecessary curly braces in most of the classes (for example :) before :
if(time == 40) 
{ 
    setImage(image1); 
} 
after :
if(time == 40) 
    setImage(image1);
I've also gave the possibility for the player to be either a boy or a girl, but I don't think it changed a lot of things, as only a few lines were added and only 8 GreenfootImage variables were created. what could cause the lag and how can I prevent it ?
RcCookie RcCookie

2021/4/6

#
The curly brackets certainly don’t make a difference. The additional pictures might though. I’m not an expert at that topic, I usually use simplistic graphics that are generated on runtime, but I’ve found my latest scenario (chrome://dino) having a pretty long loading time despite only using small images. For the lag: I don’t know at what framerate your game is running but spawning in a ton of actors can certainly cause a lot of lag. Also line 11 in the first code block has the same result as
Greenfoot.getRandomNumber(5) < 1
I think this is even quicker (but definitely not much):
Greenfoot.getRandomNumber(5) == 0
Probably quicker though is this, as it may have a native implementation:
Math.random() < 0.2
Gbasire Gbasire

2021/4/7

#
thanks, I'll try to make some changes then
Gbasire Gbasire

2021/4/7

#
made a small patch update, but it didn't really change anything. I still don't know why the worlds are taking a long time to load, and why there's lag when the player touches tallgrass.
Gbasire Gbasire

2021/4/8

#
Ok. I figured out why it lags but don't know what to do to fix it. Basically, it lags when the player adds a new Dust object into the world while walking into grass. It's really weird because it has been in the game since beginning but didn't cause any lag before. Here is the code of Dust.class :
import greenfoot.*;
public class Dust extends ObjectSpecial
{
    GreenfootImage image1 = new GreenfootImage("DustCloud/Poussiere3.png");
    GreenfootImage image2 = new GreenfootImage("DustCloud/Poussiere4.png");
    GreenfootImage image3 = new GreenfootImage("DustCloud/Poussiere5.png");
    GreenfootImage image4 = new GreenfootImage("DustCloud/Poussiere6.png");
    private int time = 12;
    public Dust()
    {
        setImage(image1);
    }
    public void act() 
    {
        time = time - 1;
        if(time == 9)
        {
            setImage(image2);
        }
        else if(time == 6)
        {
            setImage(image3);
        }
        else if(time == 3)
        {
            setImage(image4);
        }
        else if(time == 0)
        {
            getWorld().removeObject(this);
        }
    }  
}
danpost danpost

2021/4/8

#
Try making lines 4 through 7 "static" (so images are pre-loaded and all Dust actors share the same image instances).
Gbasire Gbasire

2021/4/8

#
done, but lag is still there. I tried reducing the chance of a dust being created, and it lags exclusively when a dust is added. Without any dust, there is no lag at all
Gbasire Gbasire

2021/4/8

#
I think it lags because the game already has a lot of objects in the world. The worlds load for 3-4 seconds before appearing as well, might be part of the problem
danpost danpost

2021/4/8

#
Gbasire wrote...
it lags exclusively when a dust is added. Without any dust, there is no lag at all
Provide code of ObjectSpecial class.
Gbasire Gbasire

2021/4/8

#
It is an empty class, I just created it to find the objects easily while working on the game. There's nothing in the act class, no variables etc. There is still the WorldObjects class that groups all object classes, including ObjectSpecial :
import greenfoot.*;
public class WorldObjects extends Actor
{
    public int playerx;
    public int playery;
    public int objectCenter;
    public WorldObjects()
    {
        
    }
    public void act()
    {
        
    }
    public void checkPlayer()
    {
        Boy boy = (Boy)getWorld().getObjects(Boy.class).get(0);
        playerx = boy.getX();
        playery = boy.getY();
    }
    public void collide()
    {
        Boy boy = (Boy)getWorld().getObjects(Boy.class).get(0);
        if(isTouching(Boy.class) && playery < getY() + objectCenter)
        {
            boy.hitBox();
        }
    }
    public void overlayText()
    {
        if(this.getWorld().getClass() == WorldTest.class)
        {
            ((WorldTest)getWorld()).overlayText();
        }
        else if(this.getWorld().getClass() == WorldRoute.class)
        {
            ((WorldRoute)getWorld()).overlayText();
        }
        else if(this.getWorld().getClass() == WorldTown.class)
        {
            ((WorldTown)getWorld()).overlayText();
        }
        else if(this.getWorld().getClass() == WorldCenter.class)
        {
            ((WorldCenter)getWorld()).overlayText();
        }
        else if(this.getWorld().getClass() == WorldPlayerRoom.class)
        {
            ((WorldPlayerRoom)getWorld()).overlayText();
        }
    }
}
danpost danpost

2021/4/8

#
The Dust class does not seem to make use of the WorldObjects class at all. So, that should not in any way have anything to do with the lag.
Gbasire Gbasire

2021/4/8

#
You're right, so I don't know what to do right now. Adding an object seems to make the game lag for some reason.
You need to login to post a reply.