I'm trying to move a sprite to the mouse click location. How can i get it'开发者_Python百科s coordinates?
Thanks!
Assuming you are in an event responder method (e.g. mouseDown:
):
[myView convertPointFromBase:[NSEvent mouseLocation]];
Barry's answer will work, but will return the location of the mouse at the time the function is called. This is ok as long as the event is handled shortly after it is generated, but a better approach would be:
- (void)mouseDown:(NSEvent *)event {
NSPoint location = [self convertPointFromBase:[event locationInWindow]];
//Move sprite to location
}
This will always use the location of the mouse from when the event was generated. Also, Barry's answer assumes the window and screen are using the same coordinates. This is usually true, but sometimes its better just to be safe.
精彩评论