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

2017/3/26

How can I create an enemy who is moving random and is checking for walls.

Malmi Malmi

2017/3/26

#
So I want to know how i can get my enemy moving random and cheking for walls Also he should check for Spieler(Player). When it is true he should start shooting. Is that somehow possible? As far as I am. :S
    public void RandomBewegung()
       {
        int random;
        int richtung;//direction
        random = Greenfoot.getRandomNumber(4);
        if (random==0)
        {
            richtung = 0;
            if (random == 1)
            {
                richtung = 90;
                if (random == 2)
                 {
                        richtung = 180;
                        if (random == 3)
                        {
                            richtung = 270;
                        }
                }
            }
        }
    }
    public void checkWand()
    {
       int dy=0;
       int dx=0;
        if (dx*dy != 0 || dx+dy == 0) 
        {
            return; 
        }
        setLocation(getX()+dx*2*Tempo, getY()+dy*2*Tempo);
        if (isTouching(Wand.class))
        {
            setLocation(getX()-dx*2*Tempo, getY()-dy*2*Tempo);
        }
        
    }
danpost danpost

2017/3/26

#
Okay, lets simply first:
public void RandomBewegung()
{
}

public void checkWand()
{
}
That is what you currently have. In the RandomBewegung method, you declare two local variables, and mess around with them without really doing anything with them. Actually, I could have said you had this for the method:
public void RandomBewegung()
{
    int richtung = 0;
}
But, the local variable 'richtung' is lost when the method is exited (the same with the variable 'random'). The reason the value of 'richtung' is zero is because if 'random' is any other value but zero, no code will execute; and when it is zero, you set 'richtung' to zero anyway. That is, line 9 will only be tested if 'random' is zero and the result will be 'false'; therefore, no code between lines 11 an 19 will ever execute. In the 'checkWand' method, you create two local variables ('dx' and 'dy') and set both to zero, then cause an immediate 'return' (exit out of the method) when you test 'dx+dy == 0'. So, the rest of the method will never execute and is irrelevant; 'dx' and 'dy' are lost and again, nothing is accomplished. Promote 'dx' and 'dy' to instance fields (move lines 25 and 26 to outside the method) and assign them random values in the range -1 to 1. Do you want them to move only horizontally and vertically, or would you like to include diagonal movement as well? The answer to that will determine how we will randomize 'dx' and 'dy'. Once that is done, we can occasionally alter the values (like when obstructed or world edge is encountered or just out of nowhere take a random turn). We can still employ the 'move-check obstruction-move back' procedure.
Malmi Malmi

2017/3/26

#
I want them to move to all four directions ;D
danpost danpost

2017/3/27

#
Malmi wrote...
I want them to move to all four directions ;D
I was thinking it might be easier to have a single direction field whose valid values are 0 through 3:
private int direction;
When multiplied by 90, you get the direction of movement. A simple method for movement might be:
public void move(int distance)
{
    setRotation(direction*90);
    move(distance);
    setRotation(0);
}
and a method to change direction:
private void changeDirection()
{
    direction = (direction+1+Greenfoot.getRandomNumber(3))%4;
}
Then the move-check-move back would be something like this:
int x = getX();
int y = getY();
move(2*Tempo);
if (isTouching(Wand.class))
{
    move(-2*Tempo);
    changeDirection();
]
else if (x == getX() && y == getY()) changeDirection();
else if (Greenfoot.getRandomNumber(500) < 2) changeDirection();
I added lines to change direction if an edge of the world is encountered. If it tries to move, but does not because it is moving into an edge, then the direction is changed. Also a random (whenever) turn is implemented.
You need to login to post a reply.