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

2017/3/31

Get Object at Height

rockon411 rockon411

2017/3/31

#
I'm trying to write a method that takes a y-coordinate as a parameter and returns the object at that y-coordinate. I'm supposed to write it with a loop which still kind of confuses me. Any help would be appreciated.
Super_Hippo Super_Hippo

2017/3/31

#
You could either create a for loop iterating through all possible x-coordinates and use the getObjectsAt method or you use a for each iterating through a list of all objects in the world (getObjects(null)) and you compare each y-coordinate to the parameter given. You also have to think about what should happen if more than one object has on this y-coordinate (return the first one it finds, return a random one from the ones which were found, ...) and if it should exactly be at this y-coordinate or if its image should touch it.
rockon411 rockon411

2017/3/31

#
This is what I have, but I am receiving null when there is a marble there
 public Minnow getMinnowAtHeight(int yPosition)
    {
        ocean = new Ocean();
        for (int i = 0; i <= ocean.getWidth(); i++)
        minnow = (Minnow)ocean.getOneObjectAt(i, yPosition);
        if (minnow == null)
        {
            return null;
        }
        return minnow;
    }
Super_Hippo Super_Hippo

2017/3/31

#
Well, you are creating a 'new' ocean. So this ocean is not the same as the one you see. You need to get a reference to the current world. You can remove line 6-9. Another problem is that you you are running the whole loop, so in the end it is only important if there was an object at the very last x-coordinate.
Nosson1459 Nosson1459

2017/3/31

#
Where is this getOneObjectAt method? I can't seem to find it, by World or by GreenfootImage, and those are the only places that have a getWidth() method.
danpost danpost

2017/3/31

#
@rockon411, where can one get the documentation on sofia?
rockon411 rockon411

2017/4/2

#
I went to office hours because I needed major help, but thanks for trying guys! Also I get the documentation by right clicking on world or actor and open documentation. It opens up the list of the methods in a browser.
danpost danpost

2017/4/2

#
rockon411 wrote...
I went to office hours because I needed major help, but thanks for trying guys! Also I get the documentation by right clicking on world or actor and open documentation. It opens up the list of the methods in a browser.
You misunderstood. I was not asking where you can get it -- I was asking where I can get it. Is there a site where I can go to get information on 'sofia'?
rockon411 rockon411

2017/4/2

#
World Documentation This is the website that I get when I click on documentation. It seems to be through my school so I'm not 100% sure that you will be able to access it, but have at it.
danpost danpost

2017/4/2

#
rockon411 wrote...
World Documentation This is the website that I get when I click on documentation. It seems to be through my school so I'm not 100% sure that you will be able to access it, but have at it.
I was able to view the documentation and that is all I needed to understand what you are dealing with. Thanks.
You need to login to post a reply.