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

2013/3/13

Boolean

Joorror Joorror

2013/3/13

#
I want to make a boolean! The actor have to say true if there is a wall and false if there is no wall! HELP!! Thanks! Joorror ...sorry for my English I´m just German! :)
Gevater_Tod4711 Gevater_Tod4711

2013/3/13

#
If you want your character to stop before a wall a method (which return type is boolean) would be easyer. In the method you can check whether there is a wall in front of your caracter. But to help you writing this method we need some more information about how your scenario works. For example is the cell size 1px or more (you can look it up in the world constructor), or in which directions does your caracter move?
Joorror Joorror

2013/3/13

#
My character moves 1 cell per "click", i move him with w-a-s-d! 1 cell left/right is y+/-1 and 1 cell up/down is x+/-1! But I want my character to check if there is wall ... just check! Thank You!
Gevater_Tod4711 Gevater_Tod4711

2013/3/13

#
to just check you need to use the method getOneObjectAtOffset. I think the use of a parameter would be good in this case:
public boolean nothingInTheWay(char direction) {
    switch (direction) {
        case 'w'://up;
            Actor wall = getOneObjectAtOffset(0, -1, Wall.class);
            return wall == null;
        case 'a'://left;
            Actor wall = getOneObjectAtOffset(-1, 0, Wall.class);
            return wall == null;
        case 's'://down;
            Actor wall = getOneObjectAtOffset(0, 1, Wall.class);
            return wall == null;
        case 'd'://right;
            Actor wall = getOneObjectAtOffset(1, 0, Wall.class);
            return wall == null;
        default:
            return false;
    }
}
this method will return true if there is no wall in the direction you check
You need to login to post a reply.