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

2017/3/24

Wall Collision

John10mc John10mc

2017/3/24

#
Hi, I'm new to greenfoot and I've run into a problem with Collision. I want an Actor to stop when it reaches another certain type of object. Below is the code I used to try that but it doesn't compile when I add it. Any help would be greatly appreciated.
if (Greenfoot.isKeyDown("left")) 
        {
            if (canSee(Wall.class == null))
            {
                move(0);
            }
            else
                move(-2);
        }
danpost danpost

2017/3/24

#
It is no wonder that it does not compile. 'Wall.class == null' (regardless of the fact, that it will always be 'false') returns a boolean value. However, the 'canSee' method requires a Class object for its parameter. For example:
if (canSee(Wall.class))
would compile and will execute its block of code if a Wall object intersects the actor. If the actor is to remain off of any Wall objects, then you will have to move before checking the state again (move, check and, if on a wall, move back).
You need to login to post a reply.