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

2021/6/25

Actor movement not working

Beaver Beaver

2021/6/25

#
I am working on a game, but one of my actors is not moving. In the code I have a timer, that is supposed to show the actor when to move. When the timer finishes, the actor should get a random number between 0 and 50, make an int with it plus his original coordinate and compare it with an int that shows the x coordinate he has at the moment. If the x coordinate he has is smaller or the same as the original coordinate plus the random number he should move.
import greenfoot.*;
public class Cal1 extends Actor
{
    int movetimer = 400;
    public void act()
    {
        movehorse();
    }
    public void movehorse()
    {
        movetimer--;
        if (movetimer == 0)
        {
            int d1 = Greenfoot.getRandomNumber(50);
            if (d1 == 0) d1 = Greenfoot.getRandomNumber(50);
            int x = getX();
            int xw = 64 + d1;
            if (x <= xw) move(5);
        }
    }
}


danpost danpost

2021/6/25

#
From what I see, it will take like 6 to 7 seconds before its initial moves (due to large timer value of 400), if it moves at all. Then it will not move again as timer is never reset.
You need to login to post a reply.