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

2012/3/21

help with getting objects to move towards another object

mrub4 mrub4

2012/3/21

#
I'm messing around with the little crab world from the book scenarios and i got the crab to move in random directions and now I added worms to the world. I'm trying to write code that makes the crab move towards the worm and eventually eat it if it "canSee" it and if it can't see a worm in it's path, then just keep moving. Whats the code to get the crab to move towards the worm?
Cesarferreir Cesarferreir

2012/3/31

#
import java.util.List; public int pointTo(int x, int y) { int dX = x - getX();//get distance between mouse's X and player's X int dY = y - getY();//get distance between mouse's Y and player's Y double rotation = Math.atan2(dY, dX); rotation = Math.toDegrees(rotation); return (int)rotation; } List<Worm> crab = getObjectsInRange( 2000,Worm.class); if(crab.size()!=0){ pointAtObject(crab.get(0)); move(6.0);}
You need to login to post a reply.