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

2013/5/10

Let objects in my world intersect with each other

geekykid2013 geekykid2013

2013/5/10

#
I want to have my main Actor intersect (or hit) other objects that have populated my world. How can I do this?
geekykid2013 geekykid2013

2013/5/10

#
Also I have created a grid design for a game. Inside each section of the grid I want to have an object. How do I populate my grid with several objects at once?
danpost danpost

2013/5/11

#
You could use a string whose different characters represent different object types. Iterate through the string to place the objects in the world. For example (sort of a template):
// with an instance field
final String startPosition = "aaaaaaaaabcbdbcaadbccdbaaaaaaaaa";
// in the constructor (or a method it calls)
for (int j=0; j<getHeight(); j++) for (int k=0; k<getWidth(); k++)
{
    char c = startPosition.charAt(j*getWidth()+k);
    switch ("abcd".indexOf(c))
    {
        case 0: addObject(new ObjA(), j, k); break;
        case 1: addObject(new ObjB(), j, k); break;
        case 2: addObject(new ObjC(), j, k); break;
        case 3: addObject(new ObjD(), j, k); break;
    }
}
With the above, you can place up to one object of any specified type in each grid cell in the world. The 'startPosition' string lists the object going left to right across each row from top to bottom.
You need to login to post a reply.