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

2015/11/8

Walking through a wall

USE1ESS USE1ESS

2015/11/8

#
Does any one know how to change this code to let the player stop if he hits a wall on his left side? Here's the code for stopping if the player hits a wall which is on hisright side :
public void act() 
    {
        checkRightWall(); 
    }  

public boolean checkRightWall()
    {
        int playerWidth = getImage().getWidth(); 
        int searchRightWall= (int)(playerWidth/2);
        
        Actor rightWall = getOneObjectAtOffset(searchRightWall,0,wall.class);
        
        if(rightWall == null)
        {
            return false;
        }
        else
        {
            stopAtRightWall(rightWall);
            return true;
        }
    }

 public void stopAtRightWall(Actor rightWall)
    {
       int playerWidth = rightWall.getImage().getWidth();
       int newX = rightWall.getX() -(playerWidth + getImage().getWidth())/2;   
       setLocation(newX -5,getY());
    }
USE1ESS USE1ESS

2015/11/8

#
figured it out which numbers i had to change : Line 9: int searchRightWall= (int)(playerWidth/-2); Line 27:int newX = rightWall.getX() -(playerWidth + getImage().getWidth())/-2; Line 28:setLocation(newX +5,getY());
You need to login to post a reply.