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

2011/10/23

Collision Detecting

ChrisIsOsmer ChrisIsOsmer

2011/10/23

#
Hey everyone, back again in search of some help. I need to make an object stop when it collides with a wall, but not stop the scenario and have the ability to keep moving in another direction (similar to PacMan). Can anyone help me out? Thanks in advance.
davmac davmac

2011/10/23

#
What have you got so far? Show us your code!
ChrisIsOsmer ChrisIsOsmer

2011/10/23

#
Well this is what I'm trying, but it won't compile. Keep getting this error that has something to do with the intersects. intersects(greenfoot.Actor) in greenfoot.Actor cannot be applied to (java.lang.Class<Wall>) Here's my code for the moving object. public class Pacman extends Actor { private Integer _direction; public Pacman() { _direction = 0; } /** * Act - do whatever the Pacman wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(_direction == 0) { setLocation(getX()+1, getY()); } if(_direction == 1) { setLocation(getX()-1, getY()); } if(_direction == 2) { setLocation(getX(), getY()+1); } if(_direction == 3) { setLocation(getX(), getY()-1); } movement(); } public void movement() { if(Greenfoot.isKeyDown("W")) //if(intersects(Wall.class) == false) { _direction = 3; } if(Greenfoot.isKeyDown("A")) { _direction = 1; } if(Greenfoot.isKeyDown("S")) { _direction = 2; } if(Greenfoot.isKeyDown("D")) { _direction = 0; } } } Just edited this post. What I was thinking about doing is making 4 different "collision detectors", one above, below, to the right, and left of this object. But I'm not sure how I would set their positions to this object. Anyone help with that?
davmac davmac

2011/10/24

#
Well, regarding the error: check the documentation for the "intersects" method. You need to pass it an actor, not a class. You probably meant to use "getOneIntersectingObject" instead. To check for objects above/below/left/right, you can use "getOneObjectAtOffset" (check the documentation). E.g. to look above use: getOneObjectAtOffset(0, -10, Wall.class). The "-10" means "10 cells up".
mjoraid mjoraid

2011/11/19

#
@davmac, man, you are a life saver. getOneObjectAtOffset(0, -10, Wall.class). The "-10" means "10 cells up". x and y, x is horizontal, y is vertical, but from which object ? I mean 10 cells up for .... is it the Actor, or the Wall ?
kiarocks kiarocks

2011/11/19

#
10 cells up from the actor you call it from
You need to login to post a reply.