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

2016/2/20

How I can do it my object to the other Teleports? (Sorry for my bad English)

Heliox00 Heliox00

2016/2/20

#
Sorry for my bad English. I hope that you understand what I mean. I have to do it, that my object (robot) automatically finds the object (battery). It should not matter where my object (robot) and my object (battery) is located. So when he would teleport. Have you a code or an idea how he does it?
Super_Hippo Super_Hippo

2016/2/20

#
If there is always a battery in the world, you can find it like this:
Actor battery = getWorld().getObjects(Battery.class).get(0);
Then you can get its location:
int x = battery.getX();
int y = battery.getY();
If you want to teleport the robot, you can just use the 'setLocation' method then:
setLocation(x, y);
If you want the robot to move to the battery, you can compare the 'x' and 'y' values with the robot's coordinates with simple if-conditions and let him move in the right direction.
Heliox00 Heliox00

2016/2/21

#
Thanks :D
You need to login to post a reply.