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

2023/10/12

Help

Matetity Matetity

2023/10/12

#
My character only turns when it hits one wall but doesn't turn when it hits the other. Help please! Here's the code that i have:
    public void act() 
    {
        moveAround();
    }
       public boolean hitWall()
    {
        if (isTouching (wall.class))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void moveAround()
    {
        move(3);
        if(isTouching (wall.class)==true)
        {
            move(-3);
            int i = Greenfoot.getRandomNumber(3);
            if(i==1)
        {
            setRotation(180);
            i=0;
        }
        if(i==2)
        {   
            setRotation(180);
            i=0;
        }
        }
        
    }
Borislove Borislove

2023/10/13

#
//using ternary public boolean hitWall(){ return (isTouching(Wall.class)) ? true : false; } public void moveAround(){ move(3); if(isTouching (Wall.class)){ move(-3); int i = Greenfoot.getRandomNumber(3); if(i==1){ setRotation(360); //180 ! i=0; }if(i==2){ setRotation(180); i=0; } } }
danpost danpost

2023/10/13

#
Matetity wrote...
My character only turns when it hits one wall but doesn't turn when it hits the other. Help please! Here's the code that i have: << Code Omitted >>
You are doing the same thing whether the random value is 1 or 2; plus, not doing anything if the random value is 0. But, then again, why are you using a random value at all. If the character ends up touching a wall, then in what way is the action to be performed random? Usually, tracking the direction that the character is moving will help immensely in determining what to do when encountering a wall:
private int direction = 1; // 1=right; -1 = left

public void act() {
    move(3*direction); // move
    if (isTouching(Wall.class)) { // touching wall?
        direction = -direction; // change direction
        move(3*direction); // move off wall
    }
}
That would replace your entire code given above.
Matetity Matetity

2023/10/23

#
Thanks @danpost that's a lot better then what i was doing and the only thing that it doesn't do is make the caracter turn around 180 degrees and I'm not sure how I'm supposed to do that now... Because the problem now, like before is that i only turns once.... Here's what the code is now:
public void act() {
      move(3*direction); // move
    if (isTouching(wall.class)) { // touching wall
        direction = -direction; // change direction
        move(3*direction); // move off wall
        setRotation(180);
    }
danpost danpost

2023/10/24

#
Matetity wrote...
the only thing that it doesn't do is make the caracter turn around 180 degrees and I'm not sure how I'm supposed to do that now... Because the problem now, like before is that i only turns once.... Here's what the code is now: << Code Omitted >>
With your character needing to show its moving direction by turning, that actually simplifies things. You do not need an int direction field at all. Simply this:
public void act() {
    move(3);
    if (isTouching(wall.class)) {
        turn(180);
        move(3);
    }
Matetity Matetity

2023/10/25

#
Thanks,it works now. I was clearly thinking it was more difficult then this.
You need to login to post a reply.