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

#
ummm i have one question, what exactly do i put on the etc. part? sorry if this question is pretty dumb
danpost danpost

2016/1/21

#
AngelusNeko13 wrote...
ummm i have one question, what exactly do i put on the etc. part? sorry if this question is pretty dumb
Same stuff you had after the lines that began like them before (including some of the lines after them).
AngelusNeko13 AngelusNeko13

2016/1/22

#
it still doesnt do nothing this is what i have
public class Cop extends Actor
{
     public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png");
    public boolean alive = true;
    
  
    public void act() {
       
    if (alive) {
        java.util.List<Car> playersInWorld =  getWorld().getObjects(Car.class);       
    }
}
   
      public void moveToPlayer(Car player) {
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);

      if ( alive )
        {
            Actor c = this.getOneIntersectingObject(Vehicle.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.addScore2();

                return;
            }
            Actor d = this.getOneIntersectingObject(Tree.class);
            if( d != 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 e = this.getOneIntersectingObject(Spikes.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;

        }
              Actor f = this.getOneIntersectingObject(Missiles.class);
        if( f != null)
        {
            

            
            //getWorld().removeObject(this);
            
            setImage(crashed);
            alive = false;
            World myWorld = getWorld();
            CarWorld carworld = (CarWorld)myWorld;
            Counter counter = carworld.getCounter();
            counter.addScore3();
            return;

        }
 }
}
}
danpost danpost

2016/1/22

#
I think you need to take a step backward and try again with what I suggested. Looks like you got the methods (the act and the moveToPlayer) mixed up with each other.
AngelusNeko13 AngelusNeko13

2016/1/22

#
okay ill see what i can do, and thanks alot for helping i needed to know how to do this for other game ideas
AngelusNeko13 AngelusNeko13

2016/1/22

#
okay now its following but there's an issue, when the cop hits the traffic or Vehicle class it stays in one spot or keeps following me, i need it to go down at the cars speed so here's the coding so far and thanks again
public class Cop extends Actor
{
     public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png");
    public boolean alive = true;
    
  
    public void act() {
       
    if (alive) {
        java.util.List<Car> playersInWorld =  getWorld().getObjects(Car.class);
        if ( !playersInWorld.isEmpty() )
        {
            moveToPlayer( playersInWorld.get(0) );
            
        }
    }
}
   
      public void moveToPlayer(Car player) {
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);

      if ( alive )
        {
            Actor c = this.getOneIntersectingObject(Vehicle.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.addScore2();

                return;
            }
            Actor d = this.getOneIntersectingObject(Tree.class);
            if( d != 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 e = this.getOneIntersectingObject(Spikes.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;

        }
              Actor f = this.getOneIntersectingObject(Missiles.class);
        if( f != null)
        {
            

            
            //getWorld().removeObject(this);
            
            setImage(crashed);
            alive = false;
            World myWorld = getWorld();
            CarWorld carworld = (CarWorld)myWorld;
            Counter counter = carworld.getCounter();
            counter.addScore3();
            return;

        }
 }
}
}
AngelusNeko13 AngelusNeko13

2016/1/22

#
okay nevermind i got that part but now it wont disappear when the cop is dead as in when it hits the bottom of the screen it just stays there, and sorry if im confusing you but here's my code to give you a brief summary of what i have
public class Cop extends Actor
{
     public GreenfootImage crashed = new GreenfootImage("wreckedcar2.png");
    public boolean alive = true;
    
  
    public void act() {
       
    if (alive) {
        java.util.List<Car> playersInWorld =  getWorld().getObjects(Car.class);
        if ( !playersInWorld.isEmpty() )
        {
            moveToPlayer( playersInWorld.get(0) );
            
             
        }
        
    }
     else setLocation(getX(), getY()+Car.speed );
     
}
 public void hitBottom()
    {
        if (getY()>=getWorld().getHeight()-1)
        {
            getWorld().removeObject(this);
            World myWorld = getWorld();
            CarWorld carworld = (CarWorld)myWorld;
            Counter counter = carworld.getCounter();
            counter.addScore4();
        }   
    }

   
      public void moveToPlayer(Car player) {
    turnTowards(player.getX(), player.getY());
    move(1);
    setRotation(0);

      if ( alive )
        {
            Actor c = this.getOneIntersectingObject(Vehicle.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.addScore2();

                return;
            }
            Actor d = this.getOneIntersectingObject(Tree.class);
            if( d != 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 e = this.getOneIntersectingObject(Spikes.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;

        }
              Actor f = this.getOneIntersectingObject(Missiles.class);
        if( f != null)
        {
            

            
            //getWorld().removeObject(this);
            
            setImage(crashed);
            alive = false;
            World myWorld = getWorld();
            CarWorld carworld = (CarWorld)myWorld;
            Counter counter = carworld.getCounter();
            counter.addScore3();
            return;

        }
 }
 hitBottom();
 
}
 
 
}
danpost danpost

2016/1/23

#
If you are getting an error message, you should be posting it (copying it and pasting it here). Also, you should indicate where (which line, exactly) the error is occurring). From what I can tell, you should be getting a NullPointerException on line 29 in your last code post. Once you remove 'this' from the world, 'getWorld' will not return any World object because the actor is no longer in a world. Move the line that removes 'this' from the world down past where 'getWorld' is used.
AngelusNeko13 AngelusNeko13

2016/1/23

#
okay thank you, ill see what i can do
You need to login to post a reply.
1
2