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

2015/12/20

Chase player

quadtrio quadtrio

2015/12/20

#
I have been making the enemy chase the player, but I keep getting an error which says "cannot find symbol- variable .getX();". I would appreciate any help. Thanks
davemib123 davemib123

2015/12/20

#
show the code that you are using and it will be easier to point you in the right direction
quadtrio quadtrio

2015/12/20

#
public void chasePlayer() { turnTowards(Shark.getX, Shark.getY()); move(12); }
danpost danpost

2015/12/20

#
This, 'Shark.getX', does not have a pair of round brackets following it to show that is a method call and not a variable name.
quadtrio quadtrio

2015/12/20

#
But now it is saying that non-static method getX() cannot be referenced from a static context.
danpost danpost

2015/12/20

#
You cannot call 'getX' on a Class object. It must be called on an Actor object. 'Shark' (evidently) is the name of the class that creates Shark objects; but, it is not the name of those objects created from that class. If only one Shark object is in your world, and it is there at all times any enemy object is in the world, then:
1
Actor shark = (Actor)getWorld().getObjects(Shark.class).get(0);
will get that one Shark object and name it 'shark', which can then use 'getX' on.
quadtrio quadtrio

2015/12/21

#
Thanks a lot
You need to login to post a reply.