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

2017/4/19

java.lang.OutOfMemoryError: Java heap space HELP!

1
2
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/19

#
public class Airplane extends AnimBase
{
    GreenfootSound  Bullet = new GreenfootSound ("Bullet.wav");//plays the music that i saved in the sound file
    GreenfootSound  Grenade = new GreenfootSound ("Grenade.wav");//plays the music that i saved in the sound file
    GreenfootSound  GameOver = new GreenfootSound ("GameOver.wav");//plays the music that i saved in the sound file
    GreenfootSound backgroundMusic = new GreenfootSound("Background.wav");//plays the music that i saved in the sound file

    int bulletStep=0;
    protected int Score = 0;
    protected int Point = 0;

    
    /**
     * Act - do whatever the Airplane wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
         public void stopped ()
     {
         Point = 0;
         Score = 0;
        }
    public void act() 
    {

       Point = 0;
         Score = 0;
        move (2);//keeps going forward by 2
        Actor Ok = getOneIntersectingObject(Ok.class);
                               if(Ok!= null && Greenfoot.isKeyDown("Left"))//control structure
        // if it is touching the class named Ok and the left key is being pressed                   
        {
           move (+10);//moves 10 so it can not go behind Ok class.
        }
        


   bombDetect();
      {
      catchKey();
    }
    stopped();
}
public void moveLeft()
{
    setLocation(getX()-5, getY());
}
    
    private void catchKey()
    {
        catchKeyMove();
        catchKeyShoot();
    }
    
     private void catchKeyMove()
    {
            if (Greenfoot.isKeyDown("left"))//control structure
      {
          moveLeft ();//abstraction
        }
        if (Greenfoot.isKeyDown("right"))//control structure
        {
            setLocation(getX()+5, getY());
  
        }
        if (Greenfoot.isKeyDown("Up"))//control structure
        {
            setLocation(getX(), getY()-3);
        }
        if (Greenfoot.isKeyDown("Down"))//control structure
        {
            setLocation(getX(), getY()+3);
        }
    }
    
    private void catchKeyShoot()
    {
         
        if(Greenfoot.isKeyDown("space"))//control structure
        {
            Bullet.play ();
           bulletStep = 0;
            bulletStep++;
            
            if(bulletStep==10)//control structure
            {
                Bullets b = new Bullets();
                int posX = getX()+Math.floorDiv(getImage().getWidth(),2)+Math.floorDiv(b.getImage().getWidth(),2);
                getWorld().addObject(b, posX, getY());
                bulletStep=0;
            }
        }
    }
    public void bombDetect()
    {
        GameOver gameover = new GameOver();
        if (isTouching ( Bomb.class))//control structure
        {
            
         Grenade.play ();
          Greenfoot.setWorld(gameover);  
          backgroundMusic.stop ();
          GameOver.play ();
}
}
}

Hamza_Abdulla03 Hamza_Abdulla03

2017/4/19

#
that is for the airplane
Yehuda Yehuda

2017/4/19

#
I don't see why you're showing me the Airplane class, if there is only one addObject there, and that's inside two if statements. (You should show the full class code (Ctrl+A) - starting from the imports all the way to the last line, with proper indentation (Ctrl+Shift+I in Greenfoot and Alt+Shift+F in NetBeans).)
danpost danpost

2017/4/19

#
Okay, let us simplify things a bit. I think the image handling can be much improved. You can read in the image file and keep a reference to the unmodified version of it. You only need to create another image of the same size, draw twice on it and set the background to that image:
// field
public static final GreenfootImage TILE = new GreenfootImage("space1.jpg");

// in constructor
setBackground(TILE);

// in paint method
GreenfootImage tile = new GreenfootImage(TILE.getWidth(), TILE.getHeight());
tile.drawImage(TILE, position, 0);
tile.drawImage(TILE, position+TILE.getWidth(), 0);
setBackground(tile);
Next thing is you can remoe lines 29 and 30 and change line 28 to:
if (scrollPosition > -4200) scrollPosition -= scrollSpeed;
That will stop the scrolling at a scrolling width of 5000. Now, you change line 31 to this:
paint(scrollPosition%TILE.getWidth());
The parameter value should return precisely what is needed....OH, no. Your scroll speed is either 2 or 3 and cannot be 2.5 as position and scrollPosition are int values. You will have to decide for yourself if that is alright (in which case you should covert scrollSpeed to an int, as well) or if you want to scroll at a rate of 2.5 (in which case at least scrollPosition should be a double value).
danpost danpost

2017/4/19

#
Why is there another instance of the background music in the airplane class? Music files do take up quite a bit of memory and unnecessarily having two instances of the same sound is memory wasteful -- not to mention that controlling the one in the Airplane class does nothing for the instance that is actually plaing.
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
it isnt showing the memory full thing anymore it just keeps jerking to the next movement really fast
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
danpost wrote...
Why is there another instance of the background music in the airplane class? Music files do take up quite a bit of memory and unnecessarily having two instances of the same sound is memory wasteful -- not to mention that controlling the one in the Airplane class does nothing for the instance that is actually plaing.
i need it to stop the background music
danpost danpost

2017/4/20

#
Hamza_Abdulla03 wrote...
danpost wrote...
Why is there another instance of the background music in the airplane class? Music files do take up quite a bit of memory and unnecessarily having two instances of the same sound is memory wasteful -- not to mention that controlling the one in the Airplane class does nothing for the instance that is actually plaing.
i need it to stop the background music
Place the word 'static' in from the of backgroundMusic declaration line in the Background class (line 3 in prior post of the class). Then, stop the music in the Airplane class with this line:
Background.backgroundMusic.stop();
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
none of the things you said made a difference i did all of them
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
as soon as i deleted my robot class it started working
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
public class Robot extends AnimBase
{
    /**
     * Act - do whatever the Robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    protected int Score = 0;
    public void act() 
    {
        animCounter = 0;
        
        move (20);
        
        turn(Greenfoot.getRandomNumber(20));

            {
        animCounter++;
        animMove();
        detectCollision();
    }   
} 
    private void animMove()
    {
        if(animCounter<10)
        {
            setLocation(getX(), getY()-2);
            turn(Greenfoot.getRandomNumber(10));
            Greenfoot.delay(5);
        }
        else if(animCounter<30)
        {
            setLocation(getX(), getY()+2);
            turn(Greenfoot.getRandomNumber(10));
            Greenfoot.delay(5);
        }
        else if(animCounter<40)
        {
            setLocation(getX(), getY()-2);
            turn(Greenfoot.getRandomNumber(10));
            Greenfoot.delay(5);
        }
        else
        {
            animCounter=-1;
        }
    }
    
    private void detectCollision()
    {
        if(isTouching(Bullets.class))
        {
            removeTouching(Bullets.class);
            updatePoint();
            checkLast();
            getWorld().removeObject(this);
        }
    }
    
    private void updatePoint()
    {
        
        point++;
        getWorld().showText("Score : "+point,60, 10);
    }
    YouWin youwin = new YouWin();
    private void checkLast()
    {
        if(point==30)
        {
            getWorld().showText("Finish Score "+point,getWorld().getWidth()/2, getWorld().getHeight()/2);
            removeBullet();
            removeHero();
            Greenfoot.stop();
            Greenfoot.setWorld (youwin);

        }
    }
    
    public void removeBullet()
    {
        if(getWorld().getObjects(Bullets.class)!=null)
            for(Bullets b : getWorld().getObjects(Bullets.class))
            {
                getWorld().removeObject(b);
            }
    }
    
    public void removeHero()
    {
        if(getWorld().getObjects(Airplane.class)!=null)
            getWorld().removeObject(getWorld().getObjects(Airplane.class).get(0));
    }
}


       

Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
please help need it correct by today
danpost danpost

2017/4/20

#
Your checks on lines 82 and 91 in the Robot class will always be true -- a List object is always returned (it may be an empty list; but it is a List object, nonetheless, and the existence of it, in itself, means the returned value will not be 'null'). You can simply replace lines 80 through 93 with the following:
public void removeBullet()
{
    getWorld().removeObjects(getWorld().getObjects(Bullets.class));
}

public void removeHero()
{
    getWorld().removeObjects(getWorld().getObjects(Airplane.class));
}
Some minor things you can do within the Robot class are: * remove line 11 * remove lines 17 and 21 * remove line 66 and replace line 75 with:
Greenfoot.setWorld (new YouWin());
Hamza_Abdulla03 Hamza_Abdulla03

2017/4/20

#
but that won't fix my problem
clockrandom clockrandom

2017/4/20

#
Your code is really bad and I think Mrs L***s is going to give you 1 / 25. You really need to work on your abstractions and boolean defiles, not to mention how it is possible to program the equivalent of your code in less than half the amount of code you used. Being concise is key! A fellow friend who might or might not be in your computing class...
You need to login to post a reply.
1
2