-
Creator 版本: 3.3.0
-
目标平台: Chrome 版本 93.0.4577.82(正式版本) (x86_64) macOS 11.6
getLocation返回的是物理分辨率下的OpenGL坐标:
https://github.com/cocos-creator/engine/blob/f09a8e444973dc63059622d31dc97dfb28e542ca/cocos/core/platform/event-manager/touch.ts#L61
/**
* @en Returns the current touch location in OpenGL coordinates.、
* @zh 获取当前触点位置。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocation (out?: Vec2) {
if (!out) {
out = new Vec2();
}
out.set(this._point.x, this._point.y);
return out;
}
然后getLocationInView返回的是这样的:
https://github.com/cocos-creator/engine/blob/f09a8e444973dc63059622d31dc97dfb28e542ca/cocos/core/platform/event-manager/touch.ts#L214
/**
* @en Returns the current touch location in screen coordinates.
* @zh 获取当前事件在游戏窗口内的坐标位置对象,对象包含 x 和 y 属性。
* @param out - Pass the out object to avoid object creation, very good practice
*/
public getLocationInView (out?: Vec2) {
if (!out) {
out = new Vec2();
}
out.set(this._point.x, legacyCC.view._designResolutionSize.height - this._point.y);
return out;
}
可以发现x还是getLocation的x,y是设计高度减去getLocation的y,由于设计分辨率和物理分辨率之间存在比例关系,这两个数值相减的意义是什么?
再做个实验,新建一个设计分辨率960*640的项目,监听TOUCH_START事件,分别点击画面左上角和右下角,打印出三种坐标:
getLocation 0 1280
getUILocation 0 640
getLocationInView 0 -640
getLocation 1918 2
getUILocation 959 1
getLocationInView 1918 638
(可以看出这里的物理分辨率是1920*1280)