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

2020/6/15

make actor turn at object in game

Cyten Cyten

2020/6/15

#
so for school i have to make a little red riding hood game and most of it is working but... there are trees in the game and the wolf can't go through the tree, it needs to turn to another direction when it hits/sees the tree. so in my wolf class act i now have this:
if (isTouching(Tree.class)){
             setRotation(180);
 }
but now the wolf just does a 180 and i want the wolf to turn to a random direction. The wolf already turns at the world edge to a random direction with:
    public void turnAtEdge()
    {
        int max=90;

        if(atWorldEdge())
        {
            if(getX() > 570)
            {
                max = getRandomNumber(180,200);
            }
            if(getY() > 570)
            {
                max = getRandomNumber(260,290);
            }
            if(getY() < 30)
            {
                max = getRandomNumber(60,80);
            }
            if(getX() < 30)
            {
                max = getRandomNumber(-30,30);
            }
            setRotation(max);  
        }
    }
but if i try the getRandomNumber with if (isTouching(Tree.class)) it doesn't work. Sorry if this is a dumb question i'm still learning. I tried to search the internet but i'm lost. thanks.
danpost danpost

2020/6/15

#
The setRotation method sets a specific angle to turn toward. Try the turn method instead, which changes the current rotation by a specified amount.
Cyten Cyten

2020/6/16

#
thank you so much dude.
You need to login to post a reply.