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

2017/12/11

I dont know what to do

Bandito Bandito

2017/12/11

#
so I was making my game (which really isn't very big) and I was making the 2nd wave of zombies and when it spawns in this happens Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space which is weird because of how small my game actually is please help. This is what I made when it started happening
 public void secondWave()
    {
        if(Bullet.killCount >= 9 && secondWave == 0)
        {
            for(int c = 0; c < 5; c = c++) 
            {
                int x = getWorld().getWidth();
                int y = Greenfoot.getRandomNumber(getWorld().getHeight());
                getWorld().addObject(new Zombie(), x, y);
            }
            for(int c = 0; c < 5; c = c++) 
            {
                int x = 0;
                int y = Greenfoot.getRandomNumber(getWorld().getHeight());
                getWorld().addObject(new Zombie(), x, y);
            }
            secondWave++;
        }
    }
Super_Hippo Super_Hippo

2017/12/11

#
Change 'c=c++' to 'c++'.
danpost danpost

2017/12/12

#
Super_Hippo wrote...
Change 'c=c++' to 'c++'.
I really do not think this change in code is going to change what the code does in any way. Why is this code in an Actor subclass? It probably should be in your World subclass.
Super_Hippo Super_Hippo

2017/12/12

#
The reason why I suggested it is that my Greenfoot crashes when using it.
Bandito Bandito

2017/12/12

#
Alright so I put the code into the world and added something but now it refuses to do anything at all
 public void secondWave()
    // Once the human has killed 9 zombies this adds a second wave of zombies 5 on the left and 5 on the left
    {
        if(Bullet.killCount == 9 && secondWave1 == 0)
        {
            secondWave1++;
            for(int c = 0; c < 5; c++) 
            {
                int x = getWidth();
                int y = Greenfoot.getRandomNumber(getHeight());
                addObject(new Zombie(), x, y);
            }
            for(int c = 0; c < 5; c++) 
            {
                int x = 0;
                int y = Greenfoot.getRandomNumber(getHeight());
                addObject(new Zombie(), x, y);
            }            
        }
    }
Super_Hippo Super_Hippo

2017/12/12

#
Do you call the method from the act method or when it is needed?
Bandito Bandito

2017/12/12

#
yea
Super_Hippo Super_Hippo

2017/12/12

#
Are you sure? Maybe not related to the problem, but I would also suggest to not have a static killCount variable in the Bullet class.
Bandito Bandito

2017/12/12

#
Then how would I get it to the world?
Super_Hippo Super_Hippo

2017/12/12

#
The player should could check how many zombies he killed or (since I guess there is only one player) the world could save it.
private int killCount = 0;

public void addKill()
{
    killCount++;
    secondWave();
}
If the number of kills is the only condition and the secondWave1==0 only makes sure it doesn't happen twice, then you can simply call the secondWave method from the addKill method. However, I don't believe it will be clever to have an own method and an own variable for every wave.
//when the bullet removes a zombie
((WorldName) getWorld()).addKill();
danpost danpost

2017/12/12

#
Super_Hippo wrote...
I would also suggest to not have a static killCount variable in the Bullet class.
This should not be a problem provided that the initial world constructor zeroes its value and the purpose is to count all kills by all bullets.
Bandito wrote...
Then how would I get it to the world?
However, access is not a reason for declaring a 'static' field.
danpost danpost

2017/12/12

#
Please show your world class code so it may hopefully determined why it is not working as it is intended.
Bandito Bandito

2017/12/14

#
Here is my World,
import greenfoot.*;
import javax.swing.JOptionPane;
/**
 * You are the Human, the last survivor of the zombie apocalypse. In This level you are at your home camp and you are under attack! Fend off the impending horde for as long as possible and mabye survive for another day 
 * 
 * @author Eric Faltermeier
 * @date 12/4/2017
 * 
 * I got the picure of grass from Textures101.com
 */
public class ZombieWorld extends World
{
    private int secondWave;

    /**
     * Constructor for objects of class ZombieWorld.
     * 
     */
    public ZombieWorld()
    {    
        super(1600, 900, 1);
        showInstructions();
        prepare();
        secondWave();
    }

    public void prepare()
    // This is where all the objects are added, a human, a test variable, 5 zombies on the left side, and 5 zombies on the right side
    {
        addObject(new TestVariable(), 800, 450);
        addObject(new Human(), 800, 450);
        addObject(new BulletCount(), 1590, 10);
        for(int c = 0; c < 5; c++) 
        {
            int x = getWidth();
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Zombie(), x, y);
        }
        for(int c = 0; c < 5; c++) 
        {
            int x = 0;
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Zombie(), x, y);
        }
    }

    public void showInstructions()
    // At the beginning of the game this shows up, it tells the player the goal and how to play
    {
        JOptionPane.showMessageDialog(null, "You are the last Human alive and you have to survive!\nUse WSAD to move around and space bar to shoot");
    }

    public void secondWave()
    // Once the human has killed 9 zombies this adds a second wave of zombies 5 on the left and 5 on the left
    {
        if(Bullet.killCount == 9 && secondWave == 0)
        {
            secondWave++;
            for(int c = 0; c < 5; c++) 
            {
                int x = getWidth();
                int y = Greenfoot.getRandomNumber(getHeight());
                addObject(new Zombie(), x, y);
            }
            for(int c = 0; c < 5; c++) 
            {
                int x = 0;
                int y = Greenfoot.getRandomNumber(getHeight());
                addObject(new Zombie(), x, y);
            }            
        }
    }
}
and Here is my bullet import greenfoot.*; /** * This is a bullet, it does what you would expect it to do, when it hits a zombie it kills it * * I got this picture of a bullet from pixelart.com * * @author Eric Faltermeier * @version 12/8/2017 */ public class Bullet extends Actor { public int humanRotation; static int killCount; Human hooman; public Bullet(Human hooman) //This is a constructor for bullet it resizes it, defines what a hooman is, and it makes the bullet go in the right direction { GreenfootImage myImage = getImage(); int myNewWidth = (int)myImage.getWidth()/4; int myNewHeight = (int)myImage.getHeight()/4; myImage.scale(myNewWidth,myNewHeight); this.hooman = hooman; if (hooman.direction == "a") { setRotation(180); } else if(hooman.direction == "d") { setRotation(0); } } public void act() { shoot(); move(4); } public void shoot() //This makes the mechanics of the bullet happen, it can kill a zombie and if it hits the edge then it disipears { Actor zombie = getOneIntersectingObject(Zombie.class); if(zombie != null) { getWorld().removeObject(zombie); getWorld().removeObject(this); killCount++; return; } if (isAtEdge()) { getWorld().removeObject(this); } } }
Super_Hippo Super_Hippo

2017/12/14

#
Super_Hippo wrote...
Do you call the method from the act method or when it is needed?
Bandito wrote...
yea
So where? You call it from the constructor, not from the act method.
Bandito Bandito

2017/12/14

#
alright sorry, that I answered it so late but my comment was reported for being potential spam, i figured everything out thanks though
You need to login to post a reply.