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

2019/3/13

problem with preventing player from moving if touching an object

Andrew0739 Andrew0739

2019/3/13

#
I need some help please. I'm having trouble making the player stop moving when the player touches a fence object. When the player touches the fence object initially the method bottomTouching() returns true however my first if/else statement doesn't seem to stop the player from moving forward after touching the fence. However once the player touches the fence objects, walks away, then touches it again, only then will the player be prevented from moving.
public void move(){
        Actor back = (Actor)getWorld().getObjects(background.class).get(0);
        if(Greenfoot.isKeyDown("up")){
            animate();
            setImage(user[0][currentFrame]);
            if(background.moving()){
                 setLocation(getX(), getY());
            }
            else if(!bottomTouching()){
                    setLocation(getX(), getY()-2);
            }
            direction = 0;
        }
        else if(Greenfoot.isKeyDown("right")){
            animate();
            setImage(user[1][currentFrame]);
            if(background.moving()){
                setLocation(getX(), getY());
            }
            else{
                setLocation(getX()+2, getY());
            }
            direction = 1;
            
        }
        else if(Greenfoot.isKeyDown("down")){
            animate();
            setImage(user[2][currentFrame]);
            if(background.moving()){
                setLocation(getX(), getY());
            }
            else{
                setLocation(getX(), getY()+2);
            }
            direction = 2;
            
        }
        else if(Greenfoot.isKeyDown("left")){
            animate();
            setImage(user[3][currentFrame]);
            if(background.moving()){
                setLocation(getX(), getY());
            }
            else if(!sideTouching()){
                setLocation(getX()-2, getY());
            }
            direction = 3;
            
        }
        else{
            currentFrame = 0;
            if(direction == 0){
                setImage(user[0][currentFrame]);
            }
            else if(direction == 1){
                setImage(user[1][currentFrame]);
            }
            else if(direction == 2){
                setImage(user[2][currentFrame]);
            }
            else{
                setImage(user[3][currentFrame]);
            }
        
        }
    }
 public boolean bottomTouching()
    {
        Actor fenceCheck = getOneObjectAtOffset(0, getImage().getHeight()/2-20, fence.class);
        return fenceCheck == null;
    }
Andrew0739 Andrew0739

2019/3/13

#
oj oops, i was doing some testing to see what was the problem but to no avail. I tried all the other methods to check if touching but none work. I also forgot to change my return statement for my method. It should be :
public boolean bottomTouching()
   {
       Actor fenceCheck = getOneObjectAtOffset(0, getImage().getHeight()/2-20, fence.class);
       return fenceCheck != null;
   }
danpost danpost

2019/3/13

#
In your move method: * not sure why line 2 is hanging around; * the following line, appearing in 4 different places in the method:
setLocation(getX(), getY());
leads me to believe you have overridden the setLocation method, whose code should be supplied here; * the last else block can be simplified to this:
setImage(user[direction][currentFrame]);
* movement to the right and down appear to be unrestricted when the background is not moving;
Andrew0739 Andrew0739

2019/3/14

#
Oh, ok. I have made those change. Thanks. Also i found a solution.
You need to login to post a reply.