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

2012/6/25

Mouse Pointer

Denzien Denzien

2012/6/25

#
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:
    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);
        }    
    }
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?
nccb nccb

2012/6/25

#
You're asking for all the objects of kind MouseLook, but then you are using it as if it's a Character. By the looks of it, the two types are not related. Change line 7 above to either:
MouseLook character = (MouseLook)objects.get(0);
or simply:
Actor character = (Actor)objects.get(0);
(Since you are only using it's position, it doesn't matter if you treat it as a MouseLook or as a plain old Actor)
Denzien Denzien

2012/6/26

#
Actually, what you said helped me, but only because you pointed out line 7, it was just meant to say:
MouseLook mouselook = (MouseLook)objects.get(0);
So thanks ^^ you still helped me, but just not in the way you expected :3
You need to login to post a reply.