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
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).