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

2013/2/12

Walls

Gingervitis Gingervitis

2013/2/12

#
Is there a way I could make an object like a wall so my classes cannot go right through them?
danpost danpost

2013/2/13

#
There are several Actor class methods that can be used to determine if another class of object is 'intersecting', 'inRange' or 'neighbouring'. Use one of them possibly in conjuction with a method to reposition the object to keep it off/away from the objects in question.
Gingervitis Gingervitis

2013/2/13

#
If I use one of those methods, what could I write that would make the actor stop moving when it touches it or is in the range?
danpost danpost

2013/2/13

#
Well... for example, if you use 'setLocation(getX()+dx, getY()+dy)' and 'getOneIntersectingObjects(Wall.class) != null' returns true, then 'setLocation(getX()-dx, getY()-dy)'. If you use 'move(n)', then 'move(-n)' if the return is true.
Draymothisk Draymothisk

2013/2/13

#
For a simple, beginners way of doing hit detection, try this. Actor Wall; Wall = getOneIntersectingObject(Wall.class); if(Wall != null) { //Wall is being hit. Do things here. moveRight = 0; //stop character from moving right or whatever. } else { //Wall is not being hit } Change "Wall.class" to whatever your wall class is named. The rest of "Wall", such as the first after "Actor", can be named what ever you want.
You need to login to post a reply.