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

2020/4/2

Write a new public method in your Ocean class called addMinnow(). This method should pick a random x/y location in the ocean and add a single minnow there.

Atharva1912 Atharva1912

2020/4/2

#
So I want to spawn a single fish at a random location on the map. The world is 400 x 400
public void addMinnow()
    {
        int X;
        int Y;

        for(int i = 1; i < 400; i++)
        {
            Minnow mw = new Minnow();
            X = Greenfoot.getRandomNumber(getWidth());
            Y = Greenfoot.getRandomNumber(getHeight());
            addObject(my, X, Y);
        }
    }
This is the code I have written, Idk if it works, but the
Greenfoot.getRandomNumber()
doesn't seem to work. Is there a particular class I need to import ?
danpost danpost

2020/4/2

#
Atharva1912 wrote...
So I want to spawn a single fish at a random location on the map. The world is 400 x 400 << Code Omitted >> This is the code I have written, Idk if it works, but the << Code Omitted >> doesn't seem to work. Is there a particular class I need to import ?
(1) Remove the for loop. The method is to spawn 1 minnow, not 400.. (2) The name of your minnow on line 11 does not match the name given the one created on line 8. (3) You might be getting a syntax error on X and Y saying that they may not have been initialized. To be sure, change line 3 and 4 to:
int X = 0;
int Y = 0;
Atharva1912 Atharva1912

2020/4/2

#
Thanks a lot
You need to login to post a reply.