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

2012/3/26

Moving enemies towards hero

sst0005 sst0005

2012/3/26

#
its like the old school legend of zelda, help please! im not sure what codes to use, they only have to move left right up down and move closer to 'hero' when he is in sight
danpost danpost

2012/3/26

#
You mean like: (pseudo-code) if (inRange(hero)) { int dx = hero.x - this.x, dy = hero.y - this.y; boolean useX = dx^2 > dy^2; // determines which direction is farther int velocity =(useX) ? ((dx > 0) ? 1 : -1) : ((dy > 0) ? 1 : -1); // sets right/down (1) or left/up (-1) move((useX) ? velocity : 0, (useX) ? 0 : velocity); // moves in the determined direction } The use of conditions within these statements avoid a lot of variable setting and if statements. Example (in the last statement): (useX) ? velocity : 0 'useX' returns a true/false value; if 'true', the first value after the question mark is used (velocity); if 'false', the other value is used (0).
sst0005 sst0005

2012/3/28

#
can that same program be done with the simple crabworld? like having a lobster move towards a crab but only in the up,down,left, right direction?
danpost danpost

2012/3/28

#
The possibilities are endless! (I always wanted to say that)
You need to login to post a reply.