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

2014/5/1

How do I get my enemy to chase my actor?

Fang Fang

2014/5/1

#
Another nubby question by me, but this is my second day with Greenfoot. I can make the enemy move to a certain spot and change its speed but i want the enemy to chase the actor. What is the line of code to make it do that? And how to make the actor disappear (die) when the actor touches the enemy.
danpost danpost

2014/5/1

#
Use the Actor class method 'turnTowards'. You will need to locate the player for that. You can use the Actor class method 'getObjectsInRange' which returns a List object; use the List class method 'isEmpty' with the NOT character ( '!' ) to ensure that at least one element is returned in the list; then, use the List class method 'get' on another instance of the list to get that element from the list and cast that element as an Actor object, saving it in a local variable. With that, you can use the Actor class methods 'getX' and 'getY' to get the location of the actor to use in the 'turnTowards' method. Use the Actor class method 'getWorld' to return the world it is in, followed by the World class method 'removeObject' using the 'this' keyword to have an actor remove itself from the world. Use the Actor class method 'isTouching' to determine if the actor is touching an enemy.
You need to login to post a reply.