Question

I was writing a pacman styled game in which the monsters are assumed to move in a random direction in the maze. But I can't figure it out how to make them adapt the right direction they want. I was using some constants for the directions.

public class Monster extends GObject {

    private final int DIRECTION_UP = 0;
    private final int DIRECTION_DOWN = 1;
    private final int DIRECTION_LEFT = 2;
    private final int DIRECTION_RIGHT = 3;

    private int DIRECTION = 2;

    private float speed = 0.06f;

    public Monster(float x, float y){
        super(Treasure.MONSTER_LEFT);
        setX(x);
        setY(y);
        DIRECTION = random(3);
    }

    public void update(long elapsedTime){
        setVelocityX(0);
        setVelocityY(0);
        switch (DIRECTION){
            case DIRECTION_UP :
                setVelocityY(-speed);
                setImage(Treasure.MONSTER_UP);
                break;
            case DIRECTION_DOWN :
                setVelocityY(speed);
                setImage(Treasure.MONSTER_DOWN);
                break;
            case DIRECTION_LEFT :
                setVelocityX(-speed);
                setImage(Treasure.MONSTER_LEFT);
                break;
            case DIRECTION_RIGHT :
                setVelocityX(speed);
                setImage(Treasure.MONSTER_RIGHT);
                break;
        }
        changeDirection();
    }

    private int random(int x){
        return (int)Math.floor(Math.random() * (x+1));
    }

    public void collision(GObject other){
        if (other instanceof Wall){
            switch (DIRECTION){
                case DIRECTION_UP :
                    setY(other.getY()+other.getHeight());
                    break;
                case DIRECTION_DOWN :
                    setY(other.getY()-getHeight());
                    break;
                case DIRECTION_LEFT :
                    setX(other.getX()+other.getWidth());
                    break;
                case DIRECTION_RIGHT :
                    setX(other.getX()-getWidth());
                    break;
            }
        }
    }

}

Here's the changeDirection() method I've using.

private void changeDirection(){
    int random = random(1);
    boolean opposite = (random==1);
    int dir = DIRECTION;
    switch (Explorer.DIRECTION){
        case DIRECTION_UP :
            if (opposite){
                dir = DIRECTION_DOWN;
            } else {
                dir = DIRECTION_UP;
            }
            break;
        case DIRECTION_DOWN :
            if (opposite){
                dir = DIRECTION_UP;
            } else {
                dir = DIRECTION_DOWN;
            }
            break;
        case DIRECTION_LEFT :
            if (opposite){
                dir = DIRECTION_RIGHT;
            } else {
                dir = DIRECTION_LEFT;
            }
            break;
        case DIRECTION_RIGHT :
            if (opposite){
                dir = DIRECTION_LEFT;
            } else {
                dir = DIRECTION_RIGHT;
            }
            break;
    }
    switch (dir){
        case DIRECTION_UP :
            if (Map.isObjectCollisionFree(getX(), getY()-1, true, this)){
                DIRECTION = dir;
            }
            break;
        case DIRECTION_DOWN :
            if (Map.isObjectCollisionFree(getX(), getY()+1, true, this)){
                DIRECTION = dir;
            }
            break;
        case DIRECTION_LEFT :
            if (Map.isObjectCollisionFree(getX()-1, getY(), true, this)){
                DIRECTION = dir;
            }
            break;
        case DIRECTION_RIGHT :
            if (Map.isObjectCollisionFree(getX()+1, getY(), true, this)){
                DIRECTION = dir;
            }
    }
}

Could anybody help me with how to make the monster move random in the maze...

The current issue I'm facing is that the monsters are not moving.. they are jumping in the game. If they are outside the view, they never comes back.. Monitoring them said me that their co-ordinates reach as much as -12000 and stops there. This is how I check for walls.

if (Map.isObjectCollisionFree(<x-pos>, <y-pos>, <solid-state>, <object>))

and this has worked for the player. In the collision method, I'm aligning the monster with the grid of the map.

EDIT : changed the changeDirection() method

    private void changeDirection(){     
        DIRECTION = random(3);
}

the update() method

    public void update(long elapsedTime){
    if (MapView.isVisible(this)){
        if ((getX()%Map.TILE_SIZE==0) && (getY()%Map.TILE_SIZE==0)){
            changeDirection();
        }
        setVelocityX(0);
        setVelocityY(0);
        switch (DIRECTION){
            case DIRECTION_UP :
                setVelocityY(-speed);
                setImage(Treasure.MONSTER_UP);
                break;
            case DIRECTION_DOWN :
                setVelocityY(speed);
                setImage(Treasure.MONSTER_DOWN);
                break;
            case DIRECTION_LEFT :
                setVelocityX(-speed);
                setImage(Treasure.MONSTER_LEFT);
                break;
            case DIRECTION_RIGHT :
                setVelocityX(speed);
                setImage(Treasure.MONSTER_RIGHT);
                break;
        }
    }
}
Was it helpful?

Solution

With the code you provided, there will be periods where the monster is moving in one direction, but thinks that it's moving in another (because changeDirection() changes the internal direction value, but not the monster's velocity). Then when the monster crashes into a wall while thinking that it's moving in a different direction entirely, it jumps to the side of the wall that it would be on if it were moving in the direction that it thinks it's moving... and proceeds on its merry way in the direction that it was really moving in, untill the next call to update().

To fix this, move the call to changeDirection() from the end of update() to the beginning. Actually, I'd suggest moving all of the code in update() to the end of changeDirection(), but it'll work either way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top