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

2013/10/27

Is there a method to check if my object can move?

gamer9999 gamer9999

2013/10/27

#
How can i check if my object can move? I want to avoid that i get this error fr example: Kara cant move because he cant push the mushroom! Thanks
Gevater_Tod4711 Gevater_Tod4711

2013/10/27

#
You can use the getOneObjectAtOffset(Class, int, int) method to check whether there is an object before you (using the offsets). E.g. when you want to check whether there is a mushroom infront of your actor you can use this:
if (getOneObjectAtOffset(Mushroom.class, 5, 0) {
    //there is a Musroom object on your right;
}
if (getOneObjectAtOffset(Mushroom.class, 0, 5) {
    //there is a Musroom object underneath you;
}
//...
danpost danpost

2013/10/27

#
The method Gevater_Tod4711 was referring to is 'getOneObjectAtOffset(int, int, Class)' with the offsets before the class. However, that just checks for a mushroom to one side or another. If the mushroom cannot be pushed, then there is an object (whether another mushroom or a tree or wall) that is preventing the mushroom from moving. This information (whether there is an object preventing the mushroom from moving or not) needs to be passed back to Kara so she knows whether she can push it or not. I did a duplication of a Kara program that was here on the site (I did not use or even view any of the code used in that program; I only used the level mappings with my own code and images). I believe what I did, was I put a 'move' method in the mushroom class that returned a boolean value indicating whether it was actually moved or not (and if it could, it would execute that move). That value was used in the Kara class to determine whether it should then move.
You need to login to post a reply.