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

2013/10/27

Doesn't see a left wall....

ddvink ddvink

2013/10/27

#
It's working for the right walls, but for some reason not for the left....
    // Methods for checking if the char is walking against a . If it does, it has to stop moving in the direction
    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);

        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Wall.class);

        if (rightWall == null) 
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }

    public void stopByRightWall (Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() -(wallWidth + getImage().getWidth())/2;
        setLocation(newX - 2, getY());
    }

    //niet door LINKER muur heenlopen
    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);

        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Wall.class);

        if (leftWall == null) 
        {            
            return false;
        }
        else
        {   
            System.out.println("I see a left wall");
            stopByLeftWall(leftWall);
            return true;
        }
    }

    public void stopByLeftWall (Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() +(wallWidth + getImage().getWidth())/2;
        setLocation(newX + 2, getY());
    }
Can someone explain? Yours, Dennis
Zamoht Zamoht

2013/10/27

#
Actor leftWall = getOneObjectAtOffset(xDistance, 0, Wall.class); should be Actor leftWall = getOneObjectAtOffset(-xDistance, 0, Wall.class); So your xDistance is negative and will check in the left direction.
ddvink ddvink

2013/10/27

#
You're awake!!!! Thanks a lot!
You need to login to post a reply.