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

2013/12/22

Mouse Info

cloudster101 cloudster101

2013/12/22

#
I am trying to make my player turn towards the mouse with the code
1
2
3
4
5
6
7
8
public class Player extends Actor
{
     
    public void act()
    {
        greenfoot.Actor.turnTowards(greenfoot.MouseInfo.getX(), greenfoot.MouseInfo.getX());
    }   
}
but for some reason it says getX() and getY() are non static methods but they should return numbers right.
cloudster101 cloudster101

2013/12/22

#
Sorry in the code it says getX() twice but I meant getX() and getY()
danpost danpost

2013/12/22

#
There are multiple instances of the same type problem within the code. (1) you try to call 'turnTowards' on the Actor class (2) you try to call 'getX' and 'getY' on the MouseInfo class Classes do not have locations within your world and cannot turn. Only objects created from those classes can have locations and, basically, only Actor objects can turn. That means you have to create a MouseInfo object and call the methods on the object created: 'Greenfoot.getMouseInfo()' will return a MouseInfo object that you can execute 'getX' and 'getY' on:
1
2
3
4
int mouseX = Greenfoot.getMouseInfo().getX();
// or
MouseInfo mouse = Greenfoot.getMouseInfo(); // creates the object and a reference to it ('mouse')
int mouseX = mouse.getX(); // runs method on that object
For 'turnTowards', you need a reference to an actor object to call the method on. Fortunately, when an Actor class 'act' method runs, you already have a reference to that actor with the 'this' keyword (which can be omitted, as it is understood when no object is given to run the method on):
1
2
3
this.turnTowards(mouseX, mouseY);
// or simply
turnTowards(mouseX, mouseY);
cloudster101 cloudster101

2013/12/22

#
danpost wrote...
There are multiple instances of the same type problem within the code. (1) you try to call 'turnTowards' on the Actor class (2) you try to call 'getX' and 'getY' on the MouseInfo class Classes do not have locations within your world and cannot turn. Only objects created from those classes can have locations and, basically, only Actor objects can turn. That means you have to create a MouseInfo object and call the methods on the object created: 'Greenfoot.getMouseInfo()' will return a MouseInfo object that you can execute 'getX' and 'getY' on:
1
2
3
4
int mouseX = Greenfoot.getMouseInfo().getX();
// or
MouseInfo mouse = Greenfoot.getMouseInfo(); // creates the object and a reference to it ('mouse')
int mouseX = mouse.getX(); // runs method on that object
For 'turnTowards', you need a reference to an actor object to call the method on. Fortunately, when an Actor class 'act' method runs, you already have a reference to that actor with the 'this' keyword (which can be omitted, as it is understood when no object is given to run the method on):
1
2
3
this.turnTowards(mouseX, mouseY);
// or simply
turnTowards(mouseX, mouseY);
When I did that this runtime error came up java.lang.NullPointerException at Player.<init>(Player.java:11) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.localdebugger.LocalDebugger$QueuedInstantiation.run(LocalDebugger.java:155) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/12/22

#
You will have to show your Player class code.
cloudster101 cloudster101

2013/12/22

#
This is the code for my player
1
2
3
4
5
6
7
8
9
public class Player extends Actor
{
    int mouseX = Greenfoot.getMouseInfo().getX(); 
    int mouseY = Greenfoot.getMouseInfo().getY(); 
    public void act()
    {
        this.turnTowards(mouseX, mouseY);
    }   
}
danpost danpost

2013/12/22

#
Move lines 3 and 4 to inside the 'act' method, before line 7. The 'mouseX' and 'mouseY' fields need updated each frame, or cycle, which indicates it needs to be executed from within the 'act' method.
cloudster101 cloudster101

2013/12/22

#
When I declare the variables mouseX and mouseY in the act method it won't let the program run and it gives me this runtime error java.lang.NullPointerException at Player.act(Player.java:15) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) sorry I would solve this if I could but I don't get Runtime error
danpost danpost

2013/12/22

#
That is because 'getMouseInfo' only returns an object when a mouse action is detected (the mouse is moved or a button state is changed). Your act method should be more like this:
1
2
3
4
5
6
7
8
9
public void act()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse != null)
    {
        int x = mouse.getX(), y = mouse.getY();
        turnTowards(x, y);
    }
}
cloudster101 cloudster101

2013/12/22

#
Thanks now it works :)!
You need to login to post a reply.