I want to make an enemy that charge my hero as soon as it is in the same y position. I tried a method with the getOneObjectAtOffset() method but without success. How can I solve my problem?
Hero hero = null; if (!getWorld().getObjects(Hero.class).isEmpty()) { Hero hero = (Hero) getWorld().getObjects(Hero.class).get(0); } if (hero != null && hero.getY() == getY()) { // code to charge hero }
GreenfootImage image = getImage(); setImage(new GreenfootImage(1200, 1); Actor hero = getOneIntersectingObject(Hero.class); setImage(image); if (hero != null && hero.getY() == getY()) { // code to charge hero }
if (hero != null && hero.getY() == getY() && Math.abs(hero.getX()-getX()) <= 20)