-3

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;
        }
    }
}
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • 2
    This is not a real question. You can't just dump all your code and say "please help me". – Oliver Charlesworth Jun 01 '12 at 00:03
  • 1
    Please describe the issue you are currently having. – Nick Rolando Jun 01 '12 at 00:04
  • Your problem is probably in the code that changes the monster's location, which you didn't even show. – Brilliand Jun 01 '12 at 00:13
  • @Brilliand I am using a game engine which determines the co-ordinates automatically basing on the velocities. The objects are moved automatically and any collisions are reported to the collision method where I am aligning the object to the grid by switching the direction. – Sri Harsha Chilakapati Jun 01 '12 at 00:16
  • Why do you make it go in opposite direction if random is 1? Don't you want to use random for direction? What is `Explorer.DIRECTION`?? – Nick Rolando Jun 01 '12 at 00:30
  • When the monster jumps on the map, where does it jump to? Does it always jump to another side of an adjacent wall? – Brilliand Jun 01 '12 at 00:32
  • @Shredder Explorer.DIRECTION is the current direction of the explorer. I am using opposite to make a chance of left or right to the current direction. – Sri Harsha Chilakapati Jun 01 '12 at 00:33
  • @Brilliand Yeah, it is jumping to the other side. But once it is outside the view i.e, the visible area, it dis-appears and it's coordinates decrease – Sri Harsha Chilakapati Jun 01 '12 at 00:34
  • Then I think it's the `collision()` method doing the jumping - the monsters might be managing to collide with walls that they aren't moving in the direction of. – Brilliand Jun 01 '12 at 00:37
  • Specifically, `changeDirection()` changes which way the monster thinks it's going, but not the direction it's really moving in. Then there's a collision, and the monster jumps to the side of the wall that it would be on if it was moving in the direction that it thinks it's moving. – Brilliand Jun 01 '12 at 00:39

1 Answers1

2

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.

Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • Because the other guy deleted his answer: I suggest replacing the entire first `switch` statement in `changeDirection()` with `dir = random(3);`. That will fix the problem of the monsters never turning left or right. – Brilliand Jun 01 '12 at 00:52
  • Then the problem arises again, the monsters arent coming out of the pit – Sri Harsha Chilakapati Jun 01 '12 at 00:58
  • Reducing your `changeDirection()` method to `DIRECTION = random(3);` might be overzealous - it will cause monsters to occasionally stop moving and stick their face against a wall. The code to avoid pointing a monster into a wall was a good thing. – Brilliand Jun 01 '12 at 01:22