Position of mouse click relative to scene, not window

有些话、适合烂在心里 提交于 2019-12-12 12:48:07

问题


In my game I need to use the mouse to select units.

However, I am encountering problems as I do not know how to get the coordinates of the click relative to the game, not to the window.

For example, if Unit 1 is at the (0,0) point of the game, it could be at any point on the window depending on how I pan and zoom the window, but I want the mouse click to return (0,0) no matter how I move the window as long as I click on the same spot.

Right now I am using:

override func mouseDown(with event: NSEvent) {
    eventPos = event.location(in: self)
    ...
}

(The rest of the code does not matter for the mouse click location).

This is all inside a GameScene, which is an SKScene. So how do I get the location relative to the GameScene of the mouse click?


回答1:


You need convert your point from view to scene coordinates:

eventPos = view!.convert(event.locationInWindow, to: view!.scene!)


来源:https://stackoverflow.com/questions/42911319/position-of-mouse-click-relative-to-scene-not-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!