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

2012/5/21

Mouse Direction

1
2
thefutureisnow thefutureisnow

2012/5/21

#
I'm making a game, and the character is going to be permanently in the center of the world. How can I make it that the actor turns in the direction of the mouse?
Builderboy2005 Builderboy2005

2012/5/21

#
You should check out the Actor API, there might be a method that could help you out
darkmist255 darkmist255

2012/5/21

#
Look around Greenfoot scenarios too, that's a very useful kind of thing to know, lots of people have their own attempts at it! And yeah, check out the API, there's lots of stuff in there that can be used for lots of different tasks.
thefutureisnow thefutureisnow

2012/5/21

#
Never mind, I just found out about the java.lang.Math library, there I can use the inverse tangent. Thanks!
Builderboy2005 Builderboy2005

2012/5/21

#
Lol well I was referencing the built-in command turnTowards(int x,int y) that does everything for you without needing any mathematics, but doing it by yourself is good too. Plus, knowing the math behind the routines you use is always a good thing!
darkmist255 darkmist255

2012/5/21

#
@thefutureisnow java.lang.Math contains some VERY helpful stuff for trying to do more abstract things in Java. You can accomplish a lot since you make your own methods from the ground up.
thefutureisnow thefutureisnow

2012/5/21

#
@Builderboy2005 Where is the documentation for that command?
Builderboy2005 Builderboy2005

2012/5/21

#
While in Greenfoot, right click the Actor icon, and click Open Documentation
thefutureisnow thefutureisnow

2012/5/21

#
Sorry to say this, but if I wanted to state the X and Y values of the mouse, could I write it as following?
    public void act() 
    {
        // These are declaring the X and Y variables of the mouse location as mX and mY
        int mX = MouseInfo.getX;
        int mY = MouseInfo.getY;
        
    }
Because it seems to be giving me an error in the compiling that says as following: "cannot find symbol - variable getX" And it refers to the getX in the previous code. Am I doing something wrong here?
MarkooP MarkooP

2012/5/21

#
MouseInfo.getX(); You forgot the "()"
Builderboy2005 Builderboy2005

2012/5/21

#
No, MouseInfo is a class, not an object. Methods are only avaliable to a class if they are static, and getX() and getY() are not static. In order to use getX() and getY(), you will need an object that is of the class MouseInfo. One way to get such an object is to use the Greenfoot.getMouseInfo() method. You might notice that Greenfoot is a class, and getMouseInfo() is a static method. This method returns a MouseInfo object, which we can store and then use to get the mouse information.
MouseInfo mouse;            //Going to be our MouseInfo object
int mX,mY;                    //Going to be our variables keeping track of the mouse position

public void act(){
    mouse = Greenfoot.getMouseInfo();       //Gets the mouse info
    if(mouse!=null){                    //The mouse info can sometimes be Null, so check for that
        mX = mouse.getX();            //If the mouse is not Null, update the position variables
        mY = mouse.getY();
    }
}
thefutureisnow thefutureisnow

2012/5/21

#
oops!!!, dang, that was a stupid mistake!
thefutureisnow thefutureisnow

2012/5/21

#
So do I need to create a class called "mouse" or will it automatically use the mouse?
thefutureisnow thefutureisnow

2012/5/21

#
Never mind, I've got it all now. Thanks everyone!!!
Builderboy2005 Builderboy2005

2012/5/21

#
I am unsure what you mean. I think part of the problem is that you might not know the specific differences between Classes and Objects. In the code I posted, 'MouseInfo' is a class, while 'mouse' is an object.
There are more replies on the next page.
1
2