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

2016/1/20

How to make an actor follow a player

1
2
AngelusNeko13 AngelusNeko13

2016/1/20

#
Hey i really need some help, i dont have code for this but im trying to make an actor follow the player as in follow a car, i need this actor to act as a cop trying to chase you, can anyone please give me an example?
danpost danpost

2016/1/20

#
My Zombie Shooter Demo scenario has code that relates to your issue. It would just be a matter of removing the condition of being within a range to enact following.
AngelusNeko13 AngelusNeko13

2016/1/20

#
this is what i have i keep getting error and when it works the cop doesnt crash or bump when i push itpublic class Cop extends Actor { public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png"); public boolean alive = true; public void act() { java.util.List<Car> playersInWorld = getWorld().getObjects(Car.class); if (!playersInWorld.isEmpty()) { Car player = playersInWorld.get(0); moveToPlayer(player); } if ( alive ) { Actor b = this.getOneIntersectingObject(Vehicle.class); if( b != null) { Greenfoot.playSound("Crash.wav"); //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore2(); return; } Actor c = this.getOneIntersectingObject(Tree.class); if( c != null) { Greenfoot.playSound("Crash.wav"); //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } } Actor d = this.getOneIntersectingObject(Spikes.class); if( d != null) { //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } Actor e = this.getOneIntersectingObject(Missiles.class); if( e != null) { //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } } public void moveMe() { int deltaX = getX() - player.getX(); int deltaY = getY() - player.getY(); if (Math.abs(deltaX) > Math.abs(deltaY)) { if (deltaX > 0) { setLocation(getX() - 1, getY()); } else if (deltaX < 0) { setLocation(getX() + 1, getY()); } } else { if (deltaY > 0) { setLocation(getX(), getY() - 1); } else if (deltaY < 0) { setLocation(getX(), getY() + 1); } } } public void move() { if ( alive ) moveMe(); else setLocation(getX(), getY()+Car.speed ); } }
danpost danpost

2016/1/20

#
Where is the code to the method called here?
moveToPlayer(player);
AngelusNeko13 AngelusNeko13

2016/1/20

#
oh yea i changed that to moveToPlayer(player); earlier but when i did the cop just follows but doesnt die or bounce when i bump into the cop
AngelusNeko13 AngelusNeko13

2016/1/20

#
this is what it looks like when i put what you said public class Cop extends Actor { public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png"); public boolean alive = true; public void act() { java.util.List<Car> playersInWorld = getWorld().getObjects(Car.class); if (!playersInWorld.isEmpty()) { Car player = playersInWorld.get(0); moveToPlayer(player); } if ( alive ) { Actor b = this.getOneIntersectingObject(Vehicle.class); if( b != null) { Greenfoot.playSound("Crash.wav"); //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore2(); return; } Actor c = this.getOneIntersectingObject(Tree.class); if( c != null) { Greenfoot.playSound("Crash.wav"); //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } } Actor d = this.getOneIntersectingObject(Spikes.class); if( d != null) { //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } Actor e = this.getOneIntersectingObject(Missiles.class); if( e != null) { //getWorld().removeObject(this); setImage(crashed); alive = false; World myWorld = getWorld(); CarWorld carworld = (CarWorld)myWorld; Counter counter = carworld.getCounter(); counter.addScore3(); return; } } public void moveToPlayer(Car player) { int deltaX = getX() - player.getX(); int deltaY = getY() - player.getY(); if (Math.abs(deltaX) > Math.abs(deltaY)) { if (deltaX > 0) { setLocation(getX() - 1, getY()); } else if (deltaX < 0) { setLocation(getX() + 1, getY()); } } else { if (deltaY > 0) { setLocation(getX(), getY() - 1); } else if (deltaY < 0) { setLocation(getX(), getY() + 1); } } } }
danpost danpost

2016/1/20

#
Try moving the first 5 lines of your act method to inside the following if block (where the cop is to be alive). You do not want the cop to follow if not alive, do you? I would think your 'moveToPlayer' method could be simplified by using 'turnTowards(player)' followed by moving and setting the rotation of the cop back to zero.
AngelusNeko13 AngelusNeko13

2016/1/20

#
may i have an example please? im a begginer, i get what you're saying but im kinda lost on how i would do this
danpost danpost

2016/1/20

#
AngelusNeko13 wrote...
may i have an example please? im a begginer, i get what you're saying but im kinda lost on how i would do this
For which part? simplifying the method or having the cop stop when dying?
AngelusNeko13 AngelusNeko13

2016/1/20

#
yes please
AngelusNeko13 AngelusNeko13

2016/1/20

#
both i mean please
danpost danpost

2016/1/20

#
Start the act method with:
public void act() {
    if (alive) {
        java.util.List<Car> playersInWorld = // etc.
        // 
        Actor b = // etc.
so, the cop is not following if not alive. Simplifying the 'moveToPlayer' method:
public void moveToPlayer(Car player) {
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);
}
AngelusNeko13 AngelusNeko13

2016/1/20

#
that makes alot of sense now thanks, is there more i have to do?
danpost danpost

2016/1/20

#
AngelusNeko13 wrote...
that makes alot of sense now thanks, is there more i have to do?
Well, if it is still not doing what you want, you could explain how it behaves, how it differs from what you want it to do, and show the relevant code. Please use code tags when posting code (see the link below the reply box with text 'Posting code? read this!'.
AngelusNeko13 AngelusNeko13

2016/1/20

#
okay thank you ill see what it does
There are more replies on the next page.
1
2