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

2012/2/10

Constant update of mouse info

1
2
Gazzzah Gazzzah

2012/2/10

#
I am using the Greenfoot.getMouseInfo to keep track of left / right click and mouse location. Problem is, it seems to be that objects only act when the mouse changes e.g if I click or move the mouse. Not cool because my character pivots around the mouse but moves by the keyboard. How can I make it continually check the mouse info despite it staying constant? Here is a sample of my code:
public void findMouse()
    {
        if (Greenfoot.getMouseInfo() != null)
        {
            mouseX = Greenfoot.getMouseInfo().getX();
            mouseY = Greenfoot.getMouseInfo().getY();
            gotoAngle = (int) Math.toDegrees(Math.atan2(mouseY - getY(), mouseX - getX()));
        }
    }
sp33dy sp33dy

2012/2/10

#
Seems ok to me. I need to ask, is this in your world or actor class? I certainly have no problems from the World. I'd suggest you change the code slightly:
    public void findMouse()  
        {  
            MouseInfo mi = Greenfoot.getMouseInfo();
            if (mi != null)  
            {  
                gotoAngle = (int) Math.toDegrees(Math.atan2(mi.getY() - getY(), mi.getX()- getX()));  
            }  
        }  
this compacts the code a little and you ensure you have one set of Mi coordinates. It's 'possible' that between your mouseX and mouseY variable settings, the mouse could have moved, distorting the actual position! In your gotoAngle computation, i'm not familiar with the mathematic logic there (but is y and x round the right way? It probably is, but want to check). Where are getY() and getX() coming from??? If you can answer questions, I'll try to help.. If you stick a
System.out.println(mi.getX()+" "+mi.getY()); 
before the gotoAngle, you should see the position constantly changing. If you don't, you know there is something wrong in terms of where you've put this code (i.e. actor vs world).
danpost danpost

2012/2/10

#
I think you will need to set up two integer instance variables to hold the most current mouseX and mouseY coordinates. Then when the mouseMoved(this) call returns null, you can use those values to traverse around.
Gazzzah Gazzzah

2012/2/10

#
@sp33dy This code is in an actor class, the player. The player turns to face the mouse, by working out the mouse position in relation to it's own current position and then uses the x and y distances to work out the angle to the mouse (gotoAngle).
sp33dy sp33dy

2012/2/10

#
As dan has hinted at; is your problem that the MouseInfo is sometimes null, hence the direction breaks? If so, you'll need two instance variables (i.e. private int x, y = 0) variables and set them to the last MouseInfo x and y position. You'd then add an else statement to your null check to set position based on the last recorded x/y position. If not, you need to diagnose why the mouse position isn't changing (it should). I was originally worried that your actor was't receiving the mouse input, but you aren't relying on a press, over or click (so all should be working). Are you sure your Act method is calling this method?? Otherwise, I need more information to help you :(
kiarocks kiarocks

2012/2/10

#
Not, you need the X first and the Y next, thats why it doesn't work.
danpost danpost

2012/2/10

#
No, kiarocks. Atan2 takes the y first! Check out the documentation in java.lang.math.
danpost danpost

2012/2/10

#
@Gazzzah, I think you are looking for something like the following
int goToAngle = 0;
int mseX = 0, mseY = 0;

public void findMouse()
{
    if (Greenfoot.mouseMoved(null))
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        mseX = mi.getX();
        mseY = mi.getY();
    }
    goToAngle = (int) Math.toDegrees(Math.atan2(mseY - getY(), mseX - getX()));
}
Then somewhere (after the call to the findMouse() method) you should have something to actually move the actor, like:
int r = getRotation();
setRotation(((r - goToAngle + 360) % 360 < 180) ? r - 2 : r + 2);
if (!(mseX == getX() && mseY == getY())) move(1);
}
I set it up to somewhat gradually rotate to the proper angle, and not move if actually at the location of the mouse. Oh, and yes, this would go in the class of the actor that follows the mouse.
danpost danpost

2012/2/10

#
Play around with the adjustments to r in the setRotation statement (r - 2). Try (r - 5); unless you want your actor to immediately turn toward the mouse; in which case you would just use 'setRotation(gotoAngle);'.
Gazzzah Gazzzah

2012/2/11

#
Hooray! That problem is solved. Thanks for your help guys. danpost, that turn towards equation is not what I was after but funnily enough will come in handy later, thanks. Now I'm still having one more problem with updating mouseinfo. If I click, it sets a boolean leftClicked to true (and same with rightClicked) using the following code.
        if(Greenfoot.getMouseInfo() != null)
        {
            click();
        }

    public void click()
    {
        if (Greenfoot.getMouseInfo().getButton() == 1 && released == true)
        {
            leftClicked = true;
            released = false;
        }
        if (Greenfoot.getMouseInfo().getButton() == 3)
        {
            rightClicked = true;
        }
    }
Don't worry about "released", that's not relevant. My problem is that if I left or right click it will stay that way until I move the mouse. How might I fix this?
danpost danpost

2012/2/11

#
Try
if (Greenfoot.mouseClicked(null))
in line 1. I am assuming it does not matter where the mouse is clicked, just that it is clicked. Then for your method click()
MouseInfo mi = Greenfoot.getMouseInfo();
int btn = mi.getButton();
if (btn== 1 && released)
{
    leftClicked = true;
    released = false;
}
if (btn == 3)
{
    rightClicked = true;
}
danpost danpost

2012/2/11

#
I am not sure if this is what you wanted. Your post was a bit vague. I was unsure what you meant by 'it (what) will stay that way (how) until I move the mouse'. I was thinking leftClicked and rightClicked will retain a true value??? But, if that were the case, you could always set them back to false once you have used them.
danpost danpost

2012/2/11

#
The problem, I believe, stems from the fact that you are calling getMouseInfo three different times. After the first time, it clears to null, until a mouse event occurs to put information back into it. With that being said, the code provided above should work for you.
Gazzzah Gazzzah

2012/2/11

#
Ahhh, yes that makes sense now. So the mouse info stays null until it changes. I'll try the alternative you suggested next I get the chance. And by 'it will stay that way...' I was referring to the leftClicked boolean (or rightClicked) staying true until the mouse was moved. An thus the effect of leftClicked being true continues as if held down. I'll try mouseClicked(null) and hopefully that'll work.
Gazzzah Gazzzah

2012/2/11

#
Hmm, didn't work. Now its not detecting clicks... If I right click, it triggers when I release the right click, but left click isn't working at all :S
There are more replies on the next page.
1
2