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

2018/6/13

How do I make more objects from a class appear in my world when there are none left?

jaytan jaytan

2018/6/13

#
I'm trying to make two more seagulls appear every time there is none on screen, but only two seagulls appear and fly across the screen and no other ones appear afterwards.
    private void addSeagulls()
    {
         if (getObjects(Seagull.class) == null);
         {
             addObject(new Seagull(), 999, Greenfoot.getRandomNumber(600));
             addObject(new Seagull(), 999, Greenfoot.getRandomNumber(600));    
         }
Yehuda Yehuda

2018/6/13

#
Change line 3 to:
if (getObjects(Seagull.class).isEmpty())
I think it will never be null because the list always exists, you want to see if it holds anything. Also, you have a semi-colon after the 'if'.
jaytan jaytan

2018/6/14

#
Yehuda wrote...
Change line 3 to:
if (getObjects(Seagull.class).isEmpty())
I think it will never be null because the list always exists, you want to see if it holds anything. Also, you have a semi-colon after the 'if'.
It still does the same thing, the first two fly across the screen and no more come on. Thank you though!
danpost danpost

2018/6/14

#
jaytan wrote...
It still does the same thing, the first two fly across the screen and no more come on. Thank you though!
Show your world class codes for review (there is nothing incorrect about the previous help -- so, it must be how you incorporated it).
jaytan jaytan

2018/6/15

#
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        objectFill();
        addSeagulls();
    }
    /**
     * Object that are set in the world.
     */
    public void objectFill()
    {
        addObject(new Tree(), 160,300);
        addObject(new Crosshair(), 500, 300);
        addObject(new Gun(), 500, 510);
        addObject(new Crab(), 250, 500);
    }
    /**
     * Add seagulls into the world if it is empty
     */
    private void addSeagulls()
    {
         if (getObjects(Seagull.class).isEmpty())
         {
             addObject(new Seagull(), 999, Greenfoot.getRandomNumber(600));
             addObject(new Seagull(), 999, Greenfoot.getRandomNumber(600)); 
         }
    }
danpost danpost

2018/6/15

#
jaytan wrote...
<< Code Omitted >>
Line 6 is only called when the world is constructed. To have the addSeagulls method execute while the scenario is running, it needs to be called from the act method of the class (if you do not have one in the MyWorld class, add it).
Yehuda Yehuda

2018/6/15

#
You appear to be lacking an act method. The code which you claim is not working, gets called by the constructor method, you must make an act method.
jaytan jaytan

2018/6/15

#
Thank you! It's working now
You need to login to post a reply.