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

2016/4/23

How do i use getObjectsInRange with enemy facing a certain direction, depending on what side they are on the character

FutureCoder FutureCoder

2016/4/23

#
if (!getObjectsInRange(300, Hero.class).isEmpty()) { setImage("enemy2.png"); Actor Hero = (Actor) getWorld().getObjects(Hero.class).get(0); turnTowards(Hero.getX(), Hero.getY()); move(3);*/ } Depending on what side the Hero is of the enemy, depends on the image that is set for the enemy. The problem is so far it can only be set to one image, which sometimes faces the wrong way. How do i fix this
FutureCoder FutureCoder

2016/4/23

#
ignore the */ after move(3).
FutureCoder FutureCoder

2016/4/24

#
Can someone please help me, this is a big issue with my game.
danpost danpost

2016/4/24

#
You could do something like this:
move(150);
Actor hero = null;
if (!getObjectsInRange(150, Hero.class).isEmpty())
{
    Actor hero = (Actor) getWorld().getObjects(Hero.class).get(0);
}
move(-150);
if (hero != null)
{
    turnTowards(hero.getX(), hero.getY());
    move(3);
}
What this does is move the enemy forward to check the area, then move back before adjusting its direction, if needed.
FutureCoder FutureCoder

2016/4/24

#
the enemy doesn't do anything when it is in range. I used you code but on line 5, it says variable hero has been used, so i changed it to Hero, with a capital. would that stop it from working.
danpost danpost

2016/4/24

#
FutureCoder wrote...
the enemy doesn't do anything when it is in range. I used you code but on line 5, it says variable hero has been used, so i changed it to Hero, with a capital. would that stop it from working.
No. Changing it to 'Hero' is not what is wanted. Remove the first word, 'Actor' from line 5. I declared 'hero' above it on line 2 so that its value was accessible after the 'if' block (after line 6).
FutureCoder FutureCoder

2016/4/24

#
Thanks, it works now.
You need to login to post a reply.