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

2015/12/15

Enemy moving

RaiderZ RaiderZ

2015/12/15

#
Hello citizens of the world, I have a problem with my game. I want to add a enemy who move's random on the X-axis. So some pixels left and than some pixels to the right. So I made this and he is random moving. But in the corners he is "leaving" the map and het is shaking cause he is changing the way he move's to fast. This is the code:
public void Move() {
        if(Greenfoot.getRandomNumber(500) <250) {
            move(-2);
        }
        if(Greenfoot.getRandomNumber(500) >=250) {
            move(2);
        }
    }
How can I fix this? Thanks for helping :)
danpost danpost

2015/12/15

#
RaiderZ wrote...
How can I fix this?
Actually, you cannot fix it. You need to replace it entirely. You could use an int field to hold a random x-coordinate value; have the actor move toward that location; when reached, get another random coordinate value to move toward.
You need to login to post a reply.