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

2022/5/9

I need help with a code on how I can make an actor (in this case a zombie) fall behind me when I get close to him and when I walk away he doesn't chase me

ELquenosabena ELquenosabena

2022/5/9

#
I would really appreciate it :D
danpost danpost

2022/5/9

#
ELquenosabena wrote...
I need help with a code on how I can make an actor (in this case a zombie) fall behind me when I get close to him and when I walk away he doesn't chase me
Maybe having the zombie track the distance between him and you would be sufficient. If, while in range, moving toward you does not shorten the distance, do not chase.
ELquenosabena ELquenosabena

2022/5/9

#
Thanks for answering me, but can you send me an example of a code?
danpost danpost

2022/5/9

#
ELquenosabena wrote...
can you send me an example of a code?
It would be something like the following in the zombie class:
// instance fields
private Actor player = null;
private int distanceToPlayer = 0;

// in act
if (player == null)
{
    for (Object obj : getWorld().getObjectsInRange(200, Player.class))
    {
        player = (Actor)obj;
        distanceToPlayer = (int)Math.hypot(getX()-player.getX(), getY()-player.getY());
        break;
    }
}
else 
{
    if (player.getWorld() == null || Math.hypot(getX()-player.getX(), getY()-player.getY()) > 200)
    {
        player == null;
    }
    else
    {
        int dist = (int)Math.hypot(getX()-player.getX(), getY(), player.getY());
        if (dist-2 < distanceToPlayer)
        {
            turnTowards(player.getX(), player.getY());
            move(2);
        }
        distanceToPlayer = (int)Math.hypot(getX()-player.getX(), getY()-player.getY());
}
ELquenosabena ELquenosabena

2022/5/9

#
Thank you very much, but it is giving me an error, in the part that says: player == null; , in the first = of those.
danpost danpost

2022/5/9

#
ELquenosabena wrote...
Thank you very much, but it is giving me an error, in the part that says: player == null; , in the first = of those.
Sorry, got a double push on the key. Should be:
player = null;
ELquenosabena ELquenosabena

2022/5/10

#
well, actually, I don't know if I expressed myself wrong, but, I'm just looking for an example or code that does the function that if I'm managing the "Jugador" that I have it like that (and it may give me an error for that reason) , that when something is close to the other actor (as the zombie already mentioned), that it chase itself and then if I move away from it and it continues to move randomly as I have it in this code (move(1); if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(90)-45); }) but what happens to me is that when I run it with this code (MyWorld myWorld = (MyWorld) getWorld(); Player player = myWorld.getPlayer(); this.turnTowards(player.getX(), player.getY()); ) Instead of moving randomly, he chases me automatically, I start the game and I'm not looking for him to do that, but rather start moving normally and when I get closer to him, he chases me, delete the other messages, to express myself well in this one, just in case I made myself understood, I hope and you understand, and I really wait for your answer :D
danpost danpost

2022/5/10

#
ELquenosabena wrote...
I have it in this code (move(1); if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(90)-45); })
After my given code above, add your code (quoted here) in an if block:
if (player == null)
{
    // place code here
}
ELquenosabena ELquenosabena

2022/5/11

#
Thanks for everything
You need to login to post a reply.