/** * Get the distance to the ScrollingActor. * * @return The distance between this object and the ScrollingActor. */ public double getDistanceToScrollingActor() { List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class); if (actors != null && !actors.isEmpty()) { ScrollingActor actor = null; for (ScrollingActor scrollingActor : actors) { if (scrollingActor.isScrollingCenter()) { actor = scrollingActor; } } if (actor == null) { System.err.println("No scrollingActor in the world."); return 0; } return Math.hypot(getExactX() - actor.getExactX(), getExactY() - actor.getExactY()); } return 0; }
/** * Get one component of the distance to the ScrollingActor. * * @param component You can choose which component you want. It has to be eighter 'x' or 'y'. * @return The component of the distance between the both objects. */ public double getDistanceToScrollingActor(char component) throws IllegalArgumentException { if (component == 'x') { List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class); if (actors != null && !actors.isEmpty()) { ScrollingActor actor = null; for (ScrollingActor scrollingActor : actors) { if (scrollingActor.isScrollingCenter()) { actor = scrollingActor; } } if (actor == null) { System.err.println("No scrollingActor in the world."); return 0; } return getExactX() - actor.getExactX(); } } else if (component == 'y') { List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class); if (actors != null && !actors.isEmpty()) { ScrollingActor actor = null; for (ScrollingActor scrollingActor : actors) { if (scrollingActor.isScrollingCenter()) { actor = scrollingActor; } } if (actor == null) { System.err.println("No scrollingActor in the world."); return 0; } return getExactY() - actor.getExactY(); } } else { throw new IllegalArgumentException("component (" + component + ") must be either 'x' or 'y'"); } return 0; }