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

2013/5/18

Java Heap Space - delete background image

Ragtime Ragtime

2013/5/18

#
Hi, In my game I use a scrolling method, which moves the background whenever the actor should be moved, which looks like a scrolling world. The problem is that my background is repainted in every frame, which is why the application throws the Out.Of.Memory.Error (or at least I think that's the reason). I simply haven't figured out how to delete the previous background image before the new one is painted. I would be grateful for some advice. cX and cY are the coordinates where the new background is drawn.
1
2
3
4
5
public void scrolllevel(int cX, int cY)
    {
        getBackground().clear();
        getBackground().drawImage(background,cX-500, cY-250);
    }
Thanks.
Gevater_Tod4711 Gevater_Tod4711

2013/5/18

#
The coordinates for the new background is not necessary for the heap. What is important is the data that is to be saved. Probably you are creating a new GreenfootImage every act cycle and that is to much for your ram. Could you show us the whole scrolling code?
Ragtime Ragtime

2013/5/18

#
1
2
3
4
5
6
7
public GreenfootImage background = (new GreenfootImage("level1.jpg"));
 
public void scrolllevel(int cX, int cY)
    {
        getBackground().clear();
        getBackground().drawImage(background,cX-500, cY-250);
    }
The Image public is global. This is how the method is called from the actor (car) class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void moveauto(double num)
    {
        r.move(-num); //the Actor r moves in the opposite direction the car is facing
        int xx=r.getX();
        int yy=r.getY();
        scrolllevel(xx, yy);
}
public void scrolllevel(int xx, int yy)
    {
        if(currentworld instanceof level1) ((level1) getWorld()).scrolllevel(xx, yy);
        if(currentworld instanceof level2) ((level2) getWorld()).scrolllevel(xx, yy);
        if(currentworld instanceof level3) ((level3) getWorld()).scrolllevel(xx, yy);
        if(currentworld instanceof level4) ((level4) getWorld()).scrolllevel(xx, yy);
        if(currentworld instanceof level5) ((level5) getWorld()).scrolllevel(xx, yy);
        if(currentworld instanceof tutorial) ((tutorial) getWorld()).scrolllevel(xx, yy);
    }
What might also be important is that the error occurs when I go from a level to another one.
Gevater_Tod4711 Gevater_Tod4711

2013/5/18

#
Well if you have got only one background image your code seems to be alright. If the error occours while changing the levels it's either because you add many many objects to the world for the new level (probably objects with big images) or your backgroundimage is that big that is just takes to much memory.
Ragtime Ragtime

2013/5/18

#
I will try to make some improvements. Thanks.
You need to login to post a reply.