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

2012/11/8

testing for obstacles

bonana bonana

2012/11/8

#
hey there, i'm new to greenfoot and to pragramming in general. i started editing the wombat scenario to get some expertise but know i've got a problem i'm not able to solve. i want my actors to check wether they can move or not and got some code written which (to me) seems to be correct, but greenfoot says "non-static method getX() cannot be referenced from a static context" and i don't know why. do you know a good solution for this or maybe another way to solve my problem with the obstacle checking? my code so far:
    public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        if(x = Obstacle.getX() || y = Obstacle.getY())
        {
            return false;
        }
        else
        {
            return true;
        }
    }
the problem is, i don't know how to check wether the position of my actor (after moving) would be the same as the obstacle's one.
davmac davmac

2012/11/9

#
"Obstacle" is a class; there could be any number of Obstacle objects (instances), which is why your code doesn't work - it doesn't identify which Obstacle object you want the location of. Probably want you really need to do is use the collision-checking methods to see if there are any obstacle objects at your location. The tutorials cover this (see tutorial 3).
bonana bonana

2012/11/20

#
either I don't understand you or you don't understand me. :D what I want to achieve is that my actor can't reach the position of the obstacle. so no obstacles can be at my position...
danpost danpost

2012/11/20

#
First, what is the cell size of your world? Second, what version of Greenfoot are you using?
davmac davmac

2012/11/20

#
either I don't understand you or you don't understand me. :D
Let me try again. The problem is this line:
    if(x = Obstacle.getX() || y = Obstacle.getY())  
You are saying 'Obstacle.getX()', but Obstacle is a class, not an object. There could be 0 or more objects of any class. It may be that you only have one object which is an Obstacle, but the compiler doesn't recognize that. You need to get a reference to a particular Obstacle object before you can call 'getX()' or 'getY()' on it. That is what the error message means. If what you're trying to do is see if there as an obstacle at your current position, you can just use the collision checking methods, such as 'getOneIntersectingObject(...)'. In any case, it sounds like it would be worthwhile for you to go through some of the tutorials!
You need to login to post a reply.