Hey Everyone,
I'm completely new to greenfoot (apart from the given tutorials and good java knowledge) and i'd like to know how i can add obstacles such as walls, houses or other stuff to my simulation
Would love some answers =p
Francis
I'm completely new to Greenfoot as well but... there are a few methods in the Actor class for object detection such as getOneObjectAtOffset(x,y, MyActor.cls). Basically you can say if my actor touches this portion setLocation(getX()-10,getY()-10) sort of thing. If they touch a given point then move them backwards ____ pixels with setLocation. Or just have an if statement that says if the actor goes to this x range ________________ or this y range _____________ setLocation to something further away from that object. What I said sounded pretty confusing I'm sure. There must be a more efficient way in doing so though.
I mean I had walls in my game. If my actor touched my wall while he was facing to the right I setLocation x-2 or something like that. The opposite was true if he hit a wall facing the opposite direction.
Adding them isn't difficult, but getting them to be solid so that other objects can't go through them... I'm still trying to get that answered myself.
But if you just want them to be added, put them in the constructor for your world.
let's say that your world is called ObjectWorld.
This is what the constuctor should be something like:
public ObjectWorld()
{
super(500, 500, 1); //amount of pixels (wide, tall, pixel size) for your world
addObject(new Object(), x coordinate, y coordinate);
}
And there you have a single object in the world and a world size. Of course, to make multiple objects you could put the addObject method into a 'for' loop to make multiple at once.
Again, I hope this helps you, but the objects will not, I repeat WILL NOT, be solid, and other objects will pass through them as if they weren't there. You'll need to get some other help for that part, but I hope this put you on the right path.
The guy above me might be right, never tried that myself.
This simple scenario I knocked up a while back to demonstrate how to achieve smooth movement with obstacles may help - the source is available there of course.
http://www.greenfoot.org/scenarios/524