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

2012/10/12

Memory Usage

Razzo Razzo

2012/10/12

#
How would I save Memory usage for my game? It uses tile entitys to allow for shadows but because of this the game is laggy. There is a total of 400 Tiles (20*20) Is there any way to make the game not as resource intensive My game scenario is on my profile, "Adventure"
SPower SPower

2012/10/12

#
Well, this is some advice to use less memory:
  • Don't create too much GreenfootImages. Like if you wanna change your image, you can do this (bad):
    GreenfootImage img = new GreenfootImage(50,50);
    setImage(img);
    good:
    getImage().clear();
    getImage().scale(50,50);
  • Note when you use setImage(String), a new GreenfootImage is created. If you do this often, you're creating quite some of them. It's better to create an image once and reuse that one.
  • If you don't need a GreenfootSound object anymore, call stop on it instead of pause: that will also release some more memory.
EDIT: maybe not everything is useful for you right now, but I recommand you keep it in mind in the future.
Razzo Razzo

2012/10/12

#
This is currently the code i'm using, Could you help make it more efficient?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Tile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tile extends Actor
{
    /**
     * Act - do whatever the Tile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public boolean collide;
    GreenfootImage img;
    int alpha = 255;
    static final int size = 32;
    double distToPlayer;
    public Tile(String image)
    {
        collide = false;
        img = new GreenfootImage(image+".png");
    }

    public void addedToWorld()
    {
        img.scale(size, size);
        setDistToPlayer();
        setImage(img);
        int x = getX();
        int y = getY();
        setLocation(x+((x % 16) > 8 ? (16-(x % 16)) : -(x % 16))+16, y+((y % 16) > 8 ? (16-(y % 16)) : -(y % 16))+16);
        TileRun();
    }

    Player getPlayer() {
        return Home.player;
    }

    private void setDistToPlayer() {
        distToPlayer = Math.sqrt(Math.pow(getPlayer().getX() - getX(), 2) + Math.pow(getPlayer().getY() - getY(), 2));
    }

    private void setAlpha() {
        double d = distToPlayer/200;
        if(d > 1) d = 1;
        if(d < 0) d = 0;
        alpha = (int)Math.round(255*d);
    }

    public void setImage(GreenfootImage img) {
        GreenfootImage image = new GreenfootImage(img);
        if(alpha >= 255) {
            image.fill();
        } else if(alpha > 0) {
            GreenfootImage alphaLayer = new GreenfootImage(size, size);
            alphaLayer.setColor(new Color(0, 0, 0, alpha));
            alphaLayer.fill();
            image.drawImage(alphaLayer, 0, 0);
        }
        super.setImage(image);
    }
    
    public static void align(Actor a)
    {
        int x = a.getX();
        int y = a.getY();
        a.setLocation(x+((x % 16) > 8 ? (16-(x % 16)) : -(x % 16)), y+((y % 16) > 8 ? (16-(y % 16)) : -(y % 16)));
    }

    public void TileRun()
    {
        setDistToPlayer();
        setAlpha();
        setImage(img);
    }

    public void act() 
    {
        TileRun();
    }    
}
SPower SPower

2012/10/12

#
Well, all I can see is some useless calls, like this:
public void TileRun()  
    {  
        setDistToPlayer();  
        setAlpha();  
        setImage(img); // here! 
    }
because you already set your image to img. I'll think of some things.
SPower SPower

2012/10/12

#
    public void setImage(GreenfootImage img) {
        GreenfootImage image = new GreenfootImage(img);
why would you copy the given image?
Razzo Razzo

2012/10/12

#
Because the image given is the original sprite, then It needs to add shadow onto it. I don't know how i'd do a better way.
SPower SPower

2012/10/12

#
About my first advice (useless calls) forget that one.
SPower SPower

2012/10/12

#
Razzo wrote...
Because the image given is the original sprite, then It needs to add shadow onto it. I don't know how i'd do a better way.
But you can just draw on it immediately, or would that affect other things?
Razzo Razzo

2012/10/12

#
I can't remember entirely, I think I tried that and it didn't work. Something about the alpha layer. So you know any better way to add shadows?
SPower SPower

2012/10/12

#
Maybe you could make the alphaLayer image an instance variable, instead of creating one every time. If you wanna re-fill the image, your first have to clear it.
Razzo Razzo

2012/10/12

#
I don't understand how I'd implement that into code, could you help?
SPower SPower

2012/10/12

#
// at the top of your class:
private GreenfootImage alphaLayer = new GreenfootImage(1,1);

// change the method:
    public void setImage(GreenfootImage img) {  
        GreenfootImage image = new GreenfootImage(img);  
        if(alpha >= 255) {  
            image.fill();  
        } else if(alpha > 0) {
            alphaLayer.scale(size,size);
            alphaLayer.clear();
            alphaLayer.setColor(new Color(0, 0, 0, alpha));  
            alphaLayer.fill();  
            image.drawImage(alphaLayer, 0, 0);  
        }  
        super.setImage(image);  
    }  
Note that this change wouldn't be very big.
You need to login to post a reply.