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

2014/3/26

Actor turning in direction of mouse

plant plant

2014/3/26

#
Hi, I am very new to greenfoot and only had one years experience so far. I am trying to get a Actor to always point in the direction of my cursor (mouse). How can this be done? I have tried using turnTowards(x,y) but have only managed to make the actor point to a specific coordinates. Thanks. My idea is to get a actor to shoot bullets or swing a sword in the direction of the mouse, :).
danpost danpost

2014/3/26

#
Use the Greenfoot class method 'getMouseInfo' to get a MouseInfo object and use the MouseInfo class methods on it to get the x and y you need. Refer to the API documentation of those classes.
plant plant

2014/3/26

#
I tried what you said and it does work, but only when my mouse is in the world. When my mouse goes off the borders of the world it says... java.lang.NullPointerException at Player1.turn(Player1.java:59) at Player1.act(Player1.java:13) ...I tried using a if statement, but it only turned when it fired and I still had the same problem. I just have to be careful not to move my mouse off the border. This is really weird. Can someone help?
danpost danpost

2014/3/26

#
Please post the code of your Player1 class 'act' and 'turn' methods. Use the 'code' link below the 'Post a reply' box to insert your code into the post. Specify which line is 59 in the 'turn' method of the Player1 class.
plant plant

2014/3/27

#
public void fire() { Land q = (Land)getWorld(); MouseInfo mouse = Greenfoot.getMouseInfo(); turnTowards(mouse.getX(), mouse.getY()); if (Greenfoot.isKeyDown("space")){ if(x == 0 && shot < 25 ) { shot = shot + 1; getWorld().addObject(new Bullet(getRotation()),getX(),getY()); } x = x + 1; if(x == RELOAD_SPEED) { x = 0; } } } public void reload() { shot = 0; }
danpost danpost

2014/3/28

#
You are getting an error because it is possible that the MouseInfo object returns a 'null'' value. You need to check for that befoe using any methods on it. Also, 'reload' is not being called from anywhere.
plant plant

2014/3/29

#
Sorry I dont understand.
danpost danpost

2014/3/29

#
You posted this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void fire()
{
    Land q = (Land)getWorld();
    MouseInfo mouse = Greenfoot.getMouseInfo();
    turnTowards(mouse.getX(), mouse.getY());
    if (Greenfoot.isKeyDown("space")){
         
        if(x == 0 && shot < 25 ) {
            shot = shot + 1;
            getWorld().addObject(new Bullet(getRotation()),getX(),getY()); 
        }
        x = x + 1;
        if(x == RELOAD_SPEED) {
            x = 0;
        }
    }
}
 
public void reload()
{
    shot = 0;
}
Line 5 tries to use methods on the MouseInfo object acquired on line 4. However, if the MouseInfo object is 'null', trying to execute those methods on it will throw a NullPointerException. You need to change line 6 to:
1
if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
making sure that the MouseInfo object is not null before calling the methods on it.
trias95 trias95

2014/3/29

#
1
2
3
4
5
6
MouseInfo mouse = Greenfoot.getMouseInfo();
         
        if (mouse != null)
        
           turnTowards(mouse.getX(), mouse.getY()); 
        }
Thats how i got my actor to face the mouse. Hope this helps :)
plant plant

2014/3/29

#
wow thanks im so happy now!
You need to login to post a reply.