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

2018/11/13

Wall collision - Question

Monserus Monserus

2018/11/13

#
First of - I'm pretty new to Greenfoot, so I'm very sorry if I can't understand everything, but I'll still try to follow! I've added collision to an object (a wall), so the actor can't run through it. It works fine, but everytime I want him to turn around and move in a different direction, he jumps through the wall and gets stuck behind it. I've tried to find a solution in some of the older discussions, but I wasn't able to find anything, so I'd be glad if anyone could help me with it. The wall is called "BF_MCHouseWalls_East" This line defines the speed

private int speed = 3;

This is one of my "move()" lines (there are a few more)
/**
     * moveUp - MC geht nach oben
     */
    public void moveUp()
    {
        move(speed);
        setRotation(270);        
    }
and this is the questionable line that makes the collision possible:
/**
     * MC prüft ob Mauer vorhanden
     */
    public void checkWall()
    {
        if(getOneIntersectingObject(BF_MCHouseWalls_East.class) != null)
        {
            move(-speed);
        }
    }
and this is the integration of the "collision-line" into the act-method
/**
     * Act
     */
    public void act() 
    {
        basicMovement();
        checkWall();
    }
I hope all of this is enough information to understand what I want my actor to do, and I hope someone is able to find the mistake in it. Thanks!
danpost danpost

2018/11/14

#
Monserus wrote...
I've added collision to an object (a wall), so the actor can't run through it. It works fine, but everytime I want him to turn around and move in a different direction, he jumps through the wall and gets stuck behind it.
In your move methods, you should probably set the desired rotation before moving.
You need to login to post a reply.