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

2017/3/30

Tracking and chasing actors

AustinProkopishin AustinProkopishin

2017/3/30

#
I am currently making a game for a school project and have ran into an issue. I need to make a method where one actor will get the location of another and move to that location. The purpose is to have enemies that chase the player character. I don't know how to go about doing this and would appreciate any help you could offer.
danpost danpost

2017/3/30

#
The enemies would need a reference to the player character object. This can be done by passing it to each enemy by way of a method or constructor parameter or you can acquire one by getting a list of player class objects in the world:
1
Actor player = (Actor)getWorld().getObjects(Player.class).get(0);
A player must be in the world or an exception will be thrown. Then, you can use 'player.getX()' and 'player.getY()' and have enemies turn toward (or move toward) that location. The process must be repeated each act cycle and the enemies will probably need a smooth movement engine to work with.
AustinProkopishin AustinProkopishin

2017/3/30

#
danpost wrote...
The enemies would need a reference to the player character object. This can be done by passing it to each enemy by way of a method or constructor parameter or you can acquire one by getting a list of player class objects in the world:
1
Player player = (Player)getWorld().getObjects(Player.class).get(0);
A player must be in the world or an exception will be thrown. Then, you can use 'player.getX()' and 'player.getY()' and have enemies turn toward (or move toward) that location. The process must be repeated each act cycle and the enemies will probably need a smooth movement engine to work with.
Ok, so I implemented that into my code but now I cannot place the enemy actor in the world, I get a "java null point exception" error. what do I do now?
danpost danpost

2017/3/30

#
AustinProkopishin wrote...
Ok, so I implemented that into my code but now I cannot place the enemy actor in the world, I get a "java null point exception" error. what do I do now?
Show how you implemented it (show the class code for the enemies). Also, show the exact error trace (all lines of it).
AustinProkopishin AustinProkopishin

2017/3/30

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import green foot.*; 
/**
 * The enemy ship that chases the player.
 *
 * @author Austin Prokopishin
 * @version 1.0
 */
public class alien extends Actor
{
    GifImage gifImage = new GifImage("AlienShip.gif");
    Player player = (Player)getWorld().getObjects(Player.class).get(0);
    int PlayerX = player.getX();
    int PlayerY = player.getY();
    /**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         setImage(gifImage.getCurrentImage());
         goTo();
    }
    // modified goTo from robot simulator
     public void goTo()
    {
        turnTowards(PlayerX,PlayerY);
        move(2);
         
    }
}
this is the class and I cannot copy and paste the error message but it says: "java.lang.NullPointerException at alien.<init>(alien.java:12)"
danpost danpost

2017/3/30

#
Move lines 11 through 13 to after line 25 (and before line 26).
AustinProkopishin AustinProkopishin

2017/3/30

#
It works!! thank you so much for all of your help!
You need to login to post a reply.