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

2015/8/27

addObject

1
2
Vellyxenya Vellyxenya

2015/8/27

#
Hi, in this code, i'm creating a list of monsters that are currently in the world, when i hit a monsters (code written in another class), the monster disappears. As long as it remains at least one monster in the world, the other monsters are auto-created, but when i succeed in destroying all monsters before others spawn, there're no more monsters created in the world... So what's going wrong in my code?^^
public void createMonsters()
    {
        List<Monsters> monsters = getWorld().getObjects(Monsters.class);
        Monsters monster = new Monsters();
        int lengthOfList = monsters.size();
        int randomNumber = Greenfoot.getRandomNumber(150);
        if((lengthOfList < 3 && randomNumber == 0) || monsters.isEmpty())
        {
            getWorld().addObject(monster, getWorld().getWidth(),  100+Greenfoot.getRandomNumber(getWorld().getHeight()-200));
        }
        
    }
Thank you!
danpost danpost

2015/8/27

#
The main problem is the last condition in line 7. If you do not want a Monsters object to be created when none are in the world, then you cannot use that condition.
Vellyxenya Vellyxenya

2015/8/27

#
But, I WANT a Monsters object to be created, even if there are no more in the world^^
danpost danpost

2015/8/27

#
Vellyxenya wrote...
But, I WANT a Monsters object to be created, even if there are no more in the world^^
Then what is wrong with the code as given? (at least, the logic appears correct as it is given above)
Vellyxenya Vellyxenya

2015/8/27

#
The problem is they don't respawn if i kill all of them...
Vellyxenya Vellyxenya

2015/8/27

#
and i want them to
Vellyxenya Vellyxenya

2015/8/27

#
I thought maybe the List<Monsters> monsters may disappear when they are no more monsters alive?
danpost danpost

2015/8/27

#
Vellyxenya wrote...
I thought maybe the List<Monsters> monsters may disappear when they are no more monsters alive?
The problem is that the code will not execute if no monsters are in the world because you placed the code in the Monsters class. This method should be moved to your World subclass (and adjusted to work there).
Vellyxenya Vellyxenya

2015/8/27

#
Ah okay... gonna try^^
Vellyxenya Vellyxenya

2015/8/27

#
Still doesn't work... here's my World class:
public class Game extends TheEdge
{
    
    /**
     * Constructor for objects of class Game1.
     * 
     */
    public Game()
    {
        prepare();
        createMonsters();
    }
    
    public void prepare()
    {
        Device1 device1 = new Device1();
        addObject(device1, getBackground().getWidth()/6, getBackground().getHeight()/2);
        Monsters monster1 = new Monsters();
        addObject(monster1, getBackground().getWidth()*5/6, getBackground().getHeight()/2);
        Monsters monster2 = new Monsters();
        addObject(monster2, 400, 100);
        Monsters monster3 = new Monsters();
        addObject(monster3, 500, 450);
    }
    
    public void createMonsters()
    {
        List<Monsters> monsters = getObjects(Monsters.class);
        Monsters monster = new Monsters();
        int lengthOfList = monsters.size();
        int randomNumber = Greenfoot.getRandomNumber(100);
        if((lengthOfList < 3 && randomNumber == 0) || monsters.isEmpty())
        {
            addObject(monster, getBackground().getWidth(),  100+Greenfoot.getRandomNumber(getBackground().getHeight()-200));
        }
        
    }
}
Vellyxenya Vellyxenya

2015/8/27

#
Now that i put the code in the world class, they don't respawn even if i didn't kill all the remaining monsters^^
Super_Hippo Super_Hippo

2015/8/27

#
    public Game()
    {
        prepare();
    }
    
    public void act()
    {
        createMonsters();
    }
Now? The constructor is executed only once when the object (the world) was created. The act method is executed every act-cycle.
Vellyxenya Vellyxenya

2015/8/27

#
Yea it finnally works!!! Thank you very much :D Thank you danpost too :D By the way I had another little question, in my code (not mentioned here) i use mouse.getX() ... and because of that, whenever my mouse goes out of the World, the game stops and a window with: "Greenfoot termianl window: NullPointerException"... any idea to fix that?^^
Super_Hippo Super_Hippo

2015/8/27

#
You could use something like this:
if (mouse != null)
{
    //mouse.getX()...
}
Vellyxenya Vellyxenya

2015/8/28

#
Thank you very much! :)
There are more replies on the next page.
1
2