In the game I am working on the character will turn and look in the direction of the mouse pointer (there is a class file that sticks to the pointer at all times, it looks at that) this is the code for it:
However, when I click play, I get this error message:
java.lang.ClassCastException: MouseLook cannot be cast to Character
at Character.lookAtMouse(Character.java:722)
at Character.act(Character.java:72)
at greenfoot.core.Simulation.actActor(Simulation.java:507)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
at greenfoot.core.Simulation.runContent(Simulation.java:204)
at greenfoot.core.Simulation.run(Simulation.java:194)
Help?
public void lookAtMouse()
{
List objects = getObjectsInRange(1000, MouseLook.class);
if (!objects.isEmpty())
{
Character character = (Character)objects.get(0);
int distX = character.getX() - getX();
int distY = character.getY() - getY();
double angleRadians = Math.atan2(distY, distX);
int angleDegrees = (int)Math.toDegrees(angleRadians);
setRotation(angleDegrees);
}
}

