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

2014/11/13

How to get an actor to follow another actor.

Dahoneybadger11 Dahoneybadger11

2014/11/13

#
Ok so I am making a scenario where a floating boss constantly follows you. In the human-controlled class I have:
1
2
public int grumpyX = getX();
public int grumpyY = getY();
and in the boss class I have:
1
turnTowards(GrumpyFace.grumpyX, GrumpyFace.grumpyY);
The error I keep getting says non-static variable cannot be reference from a static context Can anyone tell me what my problem is?
danpost danpost

2014/11/14

#
The problem is that your values are stored in the object created from the GrumpyFace class, not in the class itself. So, you cannot get the values by using the class name; you need a reference to the GrumpyFace object. Once you get that, you will find that you do not need fields to hold the x and y coordinates of the actor; you can get them directly from the object referenced.
1
2
GrumpyFace grumpy = /* obtain reference */;
turnTowards(grumpy.getX(), grumpy.getY());
The GrumpyFace object reference should be obtainable with this:
1
grumpy = (GrumpyFace)getWorld().getObjects(GrumpyFace.class).get(0);
Dahoneybadger11 Dahoneybadger11

2014/11/14

#
Thank-you so much! This sure was a headache -_-.
You need to login to post a reply.