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

2018/3/15

Getting a variable from an Actor class in the World class

Juno Juno

2018/3/15

#
I'm trying to make my game so if the "killcounter" variable exceeds a certain value, enemies begin appearing at an increased rate. i have tried: if (killCounter.totalCount > 50){ (where killcounter is the actor class and totalcount is the integer variable of the numbers of enemies killed) however it responds with: nonstatic variables cannot be referenced from a static context. pls help!
Super_Hippo Super_Hippo

2018/3/15

#
Save a reference to the killCounter object in your world and get the variable from this object and not from the class.
danpost danpost

2018/3/15

#
Or, use:
if ((killCounter)getObjects(killCounter.class).get(0)).totalCount > 50){
which is equivalent to:
java.util.List<killCounter> counterList = getObjects(killCounter.class);
killCounter counter = counterList.get(0);
int killCount = counter.totalCount;
if (killCount > 50){
This is presuming that exactly one killCounter object is always in the world.
You need to login to post a reply.