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

2020/12/13

Working wall

iDragon3o6 iDragon3o6

2020/12/13

#
Hello, I want to make a wall, but I don't know how I can make the player can't walk trough it...
public void move()
        {
            if(Greenfoot.isKeyDown("a"))
            {
                if(!isTouching(Wall.class))
                {
                    setLocation(getX() - 5, getY());
                }
            }
            if(Greenfoot.isKeyDown("d"))
            {
                if(!isTouching(Wall.class))
                {
                    setLocation(getX() + 5, getY());
                }
            }
        }
Roshan123 Roshan123

2020/12/13

#
Download it
iDragon3o6 iDragon3o6

2020/12/13

#
Well, I don't understand how it works...
Gbasire Gbasire

2020/12/13

#
You can do a simple thing to make some collisions in the player class, add this :
public int originalx;
public int originaly;
public void act()
{
    checkCollision();
}
public void checkCollision()
{
    originalx = getX();
    originaly = getY();
    if(isTouching(Wall.class))
    {
        setLocation(originalx, originaly);
    }
}
it will basically check where you were the second before touching the wall and warp you back there
You need to login to post a reply.