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

2015/1/20

Natural enemy movement

howdoesonecode howdoesonecode

2015/1/20

#
im working on a shooter game and i cant get the enemy random movement quite right.... it its strange and fast how they move
danpost danpost

2015/1/20

#
Cannot help without seeing what code you are using.
howdoesonecode howdoesonecode

2015/1/20

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act ()
    {
        if (atWorldEdge())
        {
            turn(17);
        }
        move(2);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(5);
        }
        if (Greenfoot.getRandomNumber(100) < 11)
        {
            turn(-5);
        }
        if (Greenfoot.getRandomNumber(700) <1)
        {
            turn(50);
        }
    }
        public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
}
danpost danpost

2015/1/20

#
You can slow them down by changing 'move(2)' to 'move(1)'. As far as the strangeness, you just have to fiddle with the turn values or add more code to be more specific as to how you want them to move.
You need to login to post a reply.