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

2015/9/30

getOneObjectAtOffset

frisostolk frisostolk

2015/9/30

#
Hi, I am making a game on greenfoot. I would like to when an object is under another object he stops falling. this is my code right now but its not working. int xwaarde=getX(); int ywaarde=getY(); ywaarde++; if (getOneObjectAtOffset( xwaarde, ywaarde / Actor.class )) {} else { setLocation(xwaarde, ywaarde); } can someone help me?
davmac davmac

2015/9/30

#
Please read this before posting code. "getOneObjectAtOffset" looks for another object at an offset from the position of the actor that you call it on. So it doesn't make sense to call getX() and getY() and use these as the offsets. If you want to see if something is "below" the current actor, use 0 as the X offset and and a small positive value as the Y offset (try 10 to start with; adjust as necessary).
1
2
3
if (getOneObjectAtOffset(0, 10, Actor.class) != null) {
    setLocation(getX(), getY() + 1);
}
You need to login to post a reply.