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

2019/11/15

Trying to minimize lag on Greenfoot website?

albertlai431 albertlai431

2019/11/15

#
Hey everyone! Just posted my simulation here: https://www.greenfoot.org/scenarios/24799 The problem is that it's quite laggy online though it runs fine on my computer. I've tried making it more efficient by limiting the number of objects made (there's rarely over 80 objects in the world at a time). Any thoughts on minimizing lag and increasing efficiency? Any suggestions would be greatly appreciated :D
nccb nccb

2019/11/15

#
For general info: Greenfoot translates into Javascript, so you can get a vague idea of what's happening if you use your browser's Javascript profiling. On Chrome, right-click page, Inspect, select Performance tab, start a recording, play scenario for 10-20 seconds, stop the recording (and pause scenario), then use mousewheel to zoom in a lot and see the method names, which correspond to your own actors' names. In your case, it looks like Timer.act is calling Timer.updateImage and all the time is spent there. First suggestion: why is updateImage() called even if the timer is not running? Second suggestion: you are adding up to 360 actors (per Timer per act()) and removing them again in the updateImage() call, just to do some trigonometry calculations, which I believe is the main problem looking at the profiling. Third suggestion: if fixing the above two doesn't fix it, it may be that the graphics operations to drag the oval are slowing things down. But I'm confident it's the adding the actors that's the issue.
albertlai431 albertlai431

2019/11/15

#
nccb wrote...
For general info: Greenfoot translates into Javascript, so you can get a vague idea of what's happening if you use your browser's Javascript profiling. On Chrome, right-click page, Inspect, select Performance tab, start a recording, play scenario for 10-20 seconds, stop the recording (and pause scenario), then use mousewheel to zoom in a lot and see the method names, which correspond to your own actors' names. In your case, it looks like Timer.act is calling Timer.updateImage and all the time is spent there. First suggestion: why is updateImage() called even if the timer is not running? Second suggestion: you are adding up to 360 actors (per Timer per act()) and removing them again in the updateImage() call, just to do some trigonometry calculations, which I believe is the main problem looking at the profiling. Third suggestion: if fixing the above two doesn't fix it, it may be that the graphics operations to drag the oval are slowing things down. But I'm confident it's the adding the actors that's the issue.
Thank you so much! Will try that out now, hope it works :D
danpost danpost

2019/11/15

#
Another thing that would help is to have the images you use for your Explosion objects already loaded as GreenfootImage objects prior to creating any explosions. Directory ops, as well as image ops are time consuming, as far as computing speeds go.
albertlai431 albertlai431

2019/11/16

#
Great, thanks! Unfortunately, it still seems to be lagging even when I use trig instead of actors, it's probably just the drawing a bunch of triangles that's slowing it down. Any suggestions on improving the timer? Also, I'm trying to figure out why it takes such a long time to make the new SpaceWorld when I click "Start Game". Inspect element showed something super interesting. In the network, it says things like ppl3.png (www.greenfoot.org). So basically, I set the image of the actors to the default greenfoot images before finding the actual images, and then afterwards I deleted those default greenfoot images from the images directory. It seems like greenfoot is still trying to access those images, like "ppl3.png", "bus-4.png","tent.png", etc. And I think that's what's making it take almost 20 sec to start the game. Would appreciate help on fixing this!
danpost danpost

2019/11/16

#
The images in your Explosion class are still being created during the creation of the each instance. Make the image array static to have them created only during compilation of the project. That way, all instances will use the same set of pre-loaded images.
albertlai431 albertlai431

2019/11/16

#
danpost wrote...
The images in your Explosion class are still being created during the creation of the each instance. Make the image array static to have them created only during compilation of the project. That way, all instances will use the same set of pre-loaded images.
Ahh, I see what you mean. Thanks! Also, I tried changing the font of Label, TextButton, and Timer to Calibri but they're still showing up as Times New Roman on the website. Am I setting the font incorrectly?
textFont = new Font ("Calibri", textSize);
img = new GreenfootImage(value, fontSize, fillColor, transparent, lineColor);
img.setFont(textFont);
setImage(img);
danpost danpost

2019/11/16

#
albertlai431 wrote...
Am I setting the font incorrectly?
You are setting the font correctly. But, maybe you do not realize that the setting of the font is for future text drawing operations. The text already drawn on the image does not change when you change the font.
albertlai431 albertlai431

2019/11/16

#
danpost wrote...
albertlai431 wrote...
Am I setting the font incorrectly?
You are setting the font correctly. But, maybe you do not realize that the setting of the font is for future text drawing operations. The text already drawn on the image does not change when you change the font.
Ah okay, got it! I've changed it to the following code but it still doesn't work on the greenfoot website. What else am I doing wrong here?
private GreenfootImage img = new GreenfootImage(1,1);
textFont = new Font ("Calibri", fontSize);
img.setFont(textFont);

//method to update image
private void updateImage()
{
    img = new GreenfootImage(value, fontSize, fillColor, transparent, lineColor);
    setImage(img);
}
danpost danpost

2019/11/16

#
albertlai431 wrote...
Ah okay, got it! I've changed it to the following code but it still doesn't work on the greenfoot website. What else am I doing wrong here? << Code Omitted >>
Now you set the font of one image (created on line 1) and then expect a new image (created on line 7) to take on that font. The key words to be aware of in my first post are "text drawing operations" (using the drawText method on a GreenfootImage object that is already created).
albertlai431 albertlai431

2019/11/16

#
Oops, sorry about that. Thanks!
albertlai431 albertlai431

2019/11/17

#
Just wondering, is there a way to set the default font so that whenever I create a Greenfoot Image, it always defaults to that font? The drawString method doesn't allow me to add a text outline so that wouldn't work for my purposes. If a default font could be set, life would be a lot easier :)
danpost danpost

2019/11/17

#
It would probably take a member of the greenfoot team to confirm, but I get the distinct feeling the answer is no. Before they went to javascript and having their own Color and Font classes, my TextImage support class would allow GreenfootImage objects to be created using a specified font. Unfortunately, it is now pretty much obsolete.
You need to login to post a reply.