鼠标单击相对于场景而不是窗口的位置 [英] Position of mouse click relative to scene, not window

查看:126
本文介绍了鼠标单击相对于场景而不是窗口的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏中,我需要使用鼠标来选择单位.

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.

例如,如果单元1位于游戏的(0,0)点,则取决于我平移和缩放窗口的方式,它可能位于窗口的任何点,但是我希望鼠标单击返回( 0,0),无论我如何移动窗口,只要单击同一位置即可.

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.

现在我正在使用:

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

(其余代码与鼠标单击位置无关).

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

所有内容都在GameScene(即SKScene)中.那么如何获得鼠标单击相对于GameScene的位置?

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?

推荐答案

您需要将点从视图转换为场景坐标:

You need convert your point from view to scene coordinates:

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

这篇关于鼠标单击相对于场景而不是窗口的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆