I’m trying to set a variable to the location of the LMB click, and keep that variable as the original click point while holding the mouse down (to calculate the difference in positions, but only while mouse is clicked).
But ActionListener continues to update the original location with the current location. To get around this I wrote this if else statement to only set the location on the first loop after clicking:
[java] if (name.equals(“LMB”) && !keyPressed){
clickCheck = false;
}
else if(name.equals(“LMB”) && clickCheck == false)
{
clickLocZero = inputManager.getCursorPosition();
clickCheck = true;
}[/java]
Maybe I’ve been overloading my brain lately or something but I can’t figure out why this code isn’t stopping the clickLocZero from being updated after clickCheck has been set to true. I thought you only got ‘!keyPressed’ once the button has been released?
By the way clickLocZero and clickCheck are both declared outside methods. Also, I checked all references in the code and there is no other place these variables are set.
What am I doing wrong? Is there a better way to calculate how far a person has dragged the cursor from original click position, while holding the button down?
Thanks
nothing will reach the second ‘else if’, and you could just use the first method
[java]
if(name.equals(“LMB”)
clicked = isPressed;
[/java]
that way when u press lmb ‘clicked’ will be true and when u release ur lmb it will be false >.>
also you can try this to get positions
[java]
if(name.equals(“LMB”)
{
clicked = isPressed;
if(isPressed)
posOne = inputManager.getCursorPosition()
else
posTwo = inputManager.getCursorPosition()
}
[/java]
Hope it helps
Thanks for the input. That looks good for getting the position where clicked and the position where released, but that’s not quite what I’m trying to do… Let me explain a bit better (I typed a bit jibberishly last night).
I want the user to click anywhere on the screen, and while holding the mouse button down still, drag the cursor around. And based on how far it is dragged from the starting point, I want to rotate an object that much, updating continually in real time. (then zero the object after releasing LMB).
Think rotating a 3d object (or the view of it) around in Blender with the middle mouse button click/drag, or in jME3 scene viewer with the lmb.
So what I need is to get the point where the click began, and hold that original location to continually compare to current location while LMB is down, so I can get the difference.
But I can’t get actionListener to update the variable holding the original location (clickLocZero) only the first loop through while holding the LMB down. It continually updates the variable with the current location! Thus the difference is always 0.
I have implemented this along with an analogListener that continually calculates the difference and moves the object based on it. For testing I also told it to System.out.println(clickLocZero + “;” + inputManager.getCursorPosition()). And it does continually while holding the button down, but both variables are printed exactly the same. For some reason ActionListener is updating clickLocZero continually while the button is pressed. (sorry for my goofy variable names).
Yeah this is a common thing with references… It’s a java quirk of sorts but you can also say its our fault.
The object you get from getCursorPosition() is also the variable that is used internally for the cursor, thus, you can’t just store it like this:
[java]
Vector2f var = inputManager.getCursorPosition();
//…
[/java]
And then expect to get the original value you had later.
A solution to this would be either to use a temp vector:
[java]
Vector2f temp = new Vector2f();
temp.set(inputManager.getCursorPosition());
//…
// access temp
[/java]
or to call clone():
[java]
Vector2f var = inputManager.getCursorPosition().clone();
[/java]
Oh sweet, all I had to do was add ‘.clone()’ in the else if block and it’s golden! thanks
On further examination I incorporated some of what Setekh said as well, I didn’t need the clickCheck because it wasn’t ActionListener that was looping and re-updating the variable. All I needed was
[java] if (name.equals(“LMB”))
clickLocZero = inputManager.getCursorPosition().clone();[/java]
Thanks again guys