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

2021/6/22

Character movement

Shinnaru Shinnaru

2021/6/22

#
Hi I want my character to move (on its own) until it hits a wall. When it hits a wall it should turn around and move in the other direction, and repeat. Thank you in advance
Shinnaru Shinnaru

2021/6/22

#
public void movement()
    {
        if(turnTimer > 0)
        {
            turnTimer--;
        }
        else if(turnTimer == 0)
        {
            turnTimer = 5;
        }

        if(isTouching(Wall.class) && turnTimer > 0)
        {  
            if(direction == 1)
            {
                direction = 0;
            }
            else
            {
                direction = 1;
            }
            turnTimer = 5;
        }

        if(direction == 1)
        {
            move(speed);
            if(animationCounter % 9 == 0)
            {
                animateRight();
            }
        }
        
        if(direction == 0)
        {
            move(-speed);
            if(animationCounter % 9 == 0)
            {
                animateLeft();
            }
        }
    }
This in my player class
Shinnaru Shinnaru

2021/6/22

#
Works now, I just noticed I had a method checkWall() which prevents my character from touching and/or going through the wall, so the if statement(Line 12) was not running.
You need to login to post a reply.