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

2019/5/1

Java Heap Space after some runs

Hanoxl Hanoxl

2019/5/1

#
Hey, I'm trying to create a rather simple version of Space Invaders and i always get the error of Java Heap Space. I tested it quite a few times and it is clear that after the fourth time I press my Play Button and after the third time i revive the error occurs. It would be really awesome if someone of you could help me :D
Hanoxl Hanoxl

2019/5/1

#
Here the code of PlayButtton
public class Play_Button extends Buttons
{
    boolean mouseOver = false;
    
    Play_Button()
    {
        setImage("Play_Button.png");
    }
    
    void isclicked()
    {
        if (Greenfoot.mouseClicked(this))
        {   
            if (getWorld() instanceof EndScreen)
            {
                getWorldOfType(EndScreen.class).make_new_Space();
            }
            else if (getWorld() instanceof Start_Screen_2ndormore)
            {
                
            }
            else
            {
                Greenfoot.setWorld(new Space(0));
            }
            
        }

    }
    
    void hover()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();  
        if (!mouseOver && Greenfoot.mouseMoved(this))  
        {  
            setImage("Play_Button2.png");  
            mouseOver = true;  
        }  
        if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))  
        {  
            setImage("Play_Button.png");  
            mouseOver = false;  
        } 
    }
    
    public void act() 
    {
        isclicked();
        hover();
    }    
}
Here the Code of Endscreen in which make_new_Space() is called
public class EndScreen extends World
{
    int highscore_saver;
    public EndScreen(int given_highscore)
    {
        super(1000,1250, 1); 
        highscore_saver = given_highscore;
    }
    void restart()
    {
        if (Greenfoot.isKeyDown("Space"))
        {
            make_new_Space();
        }
    }
    void make_new_Space()
    {
        Greenfoot.setWorld(new Space(highscore_saver));   
    }
    public void act()
    {
        restart();   
    }
}
nccb nccb

2019/5/1

#
None of this looks like a problem. What is the constructor of Space like? As a rough guess, the heap space may relate to keeping hold of a reference to the previous worlds. Likely ways for this to happen is if you keep a reference to old actors somewhere, or if you have some static variables that are keeping a reference to an old actor or old world.
Hanoxl Hanoxl

2019/5/1

#
Here it is but meanwhile I have done some changes in order to have a player that can upgrade his ship
public Space(int given_highscore, int given_lifes, int given_shoot_cooldown, int given_projectile_speed, int given_current_ship)
    {    
        super(1000,1250,1);
        
        //Player stuff
        player_lifes = given_lifes;
        player_shoot_cooldown = given_shoot_cooldown; 
        player_projectile_speed = given_projectile_speed;
        current_ship = given_current_ship;
        
        
        setBackground(bgImage);
        getBackground().drawImage(bgImage,0,-4750);
        Player p = new Player(player_lifes, player_shoot_cooldown, player_projectile_speed,current_ship);
        addObject(p,500,1000);
        addObject(score_board,750,1250); 
        
        
        highscore_saver = given_highscore;
        HighScore highscore_board = new HighScore(highscore_saver);
        addObject(highscore_board, 300, 1250);
   }
Just to be clear, how exactly if I would it look keep reference to old actors ? Should I remove objects first before I use the method setWorld() ? Thx for your help ^^
danpost danpost

2019/5/1

#
Do you have anything static in your project? Please show the declaration line for bgImage in your Space class.
Hanoxl Hanoxl

2019/5/2

#
I figured this would be enough
public class Space extends GameScreens
{
   GreenfootImage bgImage = new GreenfootImage("Space_Background.png");
   
   Score score_board = new Score();
   
   int image_count= -4750;
   
   int wave_cd = 0; 
    
   public Space(int given_highscore, int given_lifes, int given_shoot_cooldown, int given_projectile_speed, int given_current_ship)
   {
       super(given_highscore, given_lifes, given_shoot_cooldown, given_projectile_speed, given_current_ship);
       
       setBackground(bgImage);
       getBackground().drawImage(bgImage,0,-4750);
       
       Player p = new Player(player_lifes, player_shoot_cooldown, player_projectile_speed,current_ship);
       addObject(p,500,1000);
       
       addObject(score_board,750,1250); 
       
       HighScore highscore_board = new HighScore(highscore_saver);
       addObject(highscore_board, 300, 1250);
   }
   
Im currently facing another problem: I want my background to look as it would be moving. But bg_image gets faster all the time until my background become just black. It looks like there are several backgroundimages overlaying... Here the method in which i change the bg_image (it is called in act)
void background()
   {
       getBackground().drawImage(bgImage, 0, image_count);
       image_count = image_count + 5;
       if (image_count >= 253){image_count = -4750;}
   }
danpost danpost

2019/5/2

#
A way that might help heap space and fix your background issue is to create a background strip image. Since your background is to move 5 pixels at a time, create an image of world width, but only 5 pixels high. Copy bgImage onto it so that it contains the new strip that is to be across the top of the world. Then draw the current background 5 pixels down and draw the strip at the top (both onto the world background). Let image_count run from 0 to bgImage.getHeight-1.
// field
private int image_count;

void background()
{
    GreenfootImage strip = new GreenfootImage(getWidth(), 5);
    strip.drawImage(bgImage, 0, (image_count-bgImage.getHeight())%bgImage.getHeight());
    getBackground().drawImage(getBackground(), 0, 5);
    getBackground().drawImage(strip, 0, 0);
    image_count = (image_count+5)%bgImage.getHeight();
}
I believe I did that right.
Hanoxl Hanoxl

2019/5/2

#
Oh tanks for your help I never would ahve thought of something like this. I tried it as you told me to but just have a blackscreen :(
void background()
   {       
       GreenfootImage strip = new GreenfootImage(getWidth(), 5);
       strip.drawImage(bgImage, 0, (image_count-bgImage.getHeight())%bgImage.getHeight());
       getBackground().drawImage(getBackground(), 0, 5);
       getBackground().drawImage(strip, 0, 0);
       
       if (image_count >= bgImage.getHeight()-1)
       {
           image_count = 0;
       }
       else
       {
           image_count = (image_count+5)%bgImage.getHeight();
       }
   }
danpost danpost

2019/5/2

#
Hanoxl wrote...
Oh tanks for your help I never would ahve thought of something like this. I tried it as you told me to but just have a blackscreen :( << Code Omitted >>
I wonder why you changed the code. Remove lines 8 thru 13 and line 15.
Hanoxl Hanoxl

2019/5/2

#
Oh I'm sry I thought you told me to write this code myself
danpost wrote...
Let image_count run from 0 to bgImage.getHeight-1.
I have now just copy and pasted but there is still just a blackscreen Could it be important that our image is much longer then the height of the world ? (1000,6250 Pixel)
void background()
   {       
       GreenfootImage strip = new GreenfootImage(getWidth(), 5);
       strip.drawImage(bgImage, 0, (image_count-bgImage.getHeight())%bgImage.getHeight());
       getBackground().drawImage(getBackground(), 0, 5);
       getBackground().drawImage(strip, 0, 0);
       image_count = (image_count+5)%bgImage.getHeight();
       
   }
After i removed the following part from my constructor there is some kind of flickering
setBackground(bgImage);
getBackground().drawImage(bgImage,0,-4750);
danpost danpost

2019/5/2

#
Hanoxl wrote...
After i removed the following part from my constructor there is some kind of flickering << Code Omitted >>
I was about to tell you to remove line 19 (the first of the two lines you removed). Line 20 can just be:
getBackground().drawImage(bgImage, 0, image_count);
And I found the following should then work:
void background()
{
    image_count = (image_count+5)%bgImage.getHeight();
    getBackground().drawImage(bgImage, 0, image_count-bgImage.getHeight());
    getBackground().drawImage(bgImage, 0, image_count);
}
Hanoxl Hanoxl

2019/5/2

#
WOW I can't thank you enough danpost. You are the holy greenfoot legend <3 Have a nice day !
You need to login to post a reply.