0

This is what I'm trying to accomplish. I want an item to show up on the screen at the last position the mouse had clicked. Currently it sort of works, but every time I click elsewhere the said item moves there as well. I just want it to be static.

Here's some code:

global variables px and py. Used to hold mouse clicks:

private int px = 250; // initial coordinates
private int py = 250;

mouse clicks feed global px and py:

private void testPress(int x, int y) {
    if (!isPaused && !gameOver) {
        // do something..
        px = x;
        py = y;
        girlP.setDestination((px-(girlP.getImage().getWidth(this)/2)), 
                (py-(girlP.getImage().getHeight(this)/2)));

        //System.out.println(px + ", " + py);
    }
}

There px and py are constantly updated with new values. What I want is a way to only hold the last mouse click

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
blutuu
  • 191
  • 2
  • 12
  • 1
    You want it at the last clicked position, but not to move when you click? Your question doesn't make sense at the moment. – Keppil Apr 09 '13 at 04:40
  • @keppil Yeah it is kind of confusing. Basically, I have a character that moves to wherever the mouse specifies. The character is to have the ability to plant flowers. What I want to do plant a flower wherever the character is standing (i.e. the last mouse point). – blutuu Apr 09 '13 at 04:43
  • For one, you need to define some kind of 'pinning' action - aka, what distinguishes between a mouse click that should 'pin' your item to a spot, vs a mouse click that shouldn't. Also, you shouldn't use global state unless you expect to only ever have the one item that will behave as described. – Perception Apr 09 '13 at 04:46
  • @Perception What if I used an array somehow? – blutuu Apr 09 '13 at 05:04
  • @blutuu - I don't see how an array will help with what is a more fundamental design problem. Now, if you wanted to store a history of clicked locations, then yea, storing points in a (size-bounded) array would be ok, but that doesn't address the original issue, which is - 'whats the difference between a click that pins and a click that doesnt'? As an example of a possible solution, you could define right clicks to pin, and left clicks to do 'whatever else'. – Perception Apr 09 '13 at 05:07

1 Answers1

0

See this question to understand how to listen for mouse events

You can use event.clickX and event.clickY to get the coordinates within the element you listen for mouse click on, or use event.screenX and event.screenY to get the absolute mouse coordinates within the document.

Community
  • 1
  • 1
Ben Barkay
  • 5,473
  • 2
  • 20
  • 29