[LayaAir2]多指触摸在电脑上检测不出来,理论上要检测出来才对的!
引擎版本 2.13.3
一直有这个问题,PC上检测不出多指的触摸的,必须是手机模式才可以,这个是属于 BUG 吗?
一直有这个问题,PC上检测不出多指的触摸的,必须是手机模式才可以,这个是属于 BUG 吗?
import Input3D = Laya.Input3D;
import Vector2 = Laya.Vector2;
/**
* 手指。
*/
export class Finger {
public id: number = 0;
public position: Vector2 = new Vector2();
public deltaPosition: Vector2 = new Vector2();
constructor(id?: number) {
if (id != undefined) {
this.id = id;
}
}
}
/**
* 触摸监听者接口。
*/
export interface ITouchListener {
onTouchDown(finger: Finger);
onTouchSwipe(finger: Finger);
onTouchUp(finger: Finger);
}
/**
* 多点触摸。
*/
export class Touch {
private input: Input3D;
private touchCount: number = 0;
private touches: Map<number, Laya.Touch> = new Map<number, Laya.Touch>();
private oldTouches: Map<number, Laya.Touch> = new Map<number, Laya.Touch>();
private fingers: Map<number, Finger> = new Map<number, Finger>();
private listener: ITouchListener;
constructor(input: Input3D) {
this.input = input;
if (Laya.Browser.onPC) {
}
}
dispose() {
if (Laya.Browser.onPC) {
}
}
public setListener(listener: ITouchListener) {
this.listener = listener;
}
/** 帧更新函数中调用。 */
public update() {
this.touchCount = this.input.touchCount(); // 这个在 PC 上永远是 0。
// copy touches.
if (this.oldTouches.size > 0) {
this.oldTouches.clear();
}
for (let [key, value] of this.touches) {
this.oldTouches.set(key, value);
}
// reset touches.
if (this.touches.size > 0) {
this.touches.clear();
}
// get touches.
for (let i = 0; i < this.touchCount; ++i) {
let touch = this.input.getTouch(i);
// this.touches.set(touch.identifier, touch); // iOS 上是一个不确定的很小的负数,并不是0开始的,且每点击一次都会-1,所以有问题。
this.touches.set(i, touch);
}
// fingers gesture.
for (let [key, value] of this.touches) {
// get finger.
let finger = this.fingers.get(key)
if (finger == undefined) {
finger = new Finger();
finger.id = key;
this.fingers.set(key, finger);
}
if (this.oldTouches.has(key)) {
// downing.
finger.deltaPosition.x = value.position.x - finger.position.x;
finger.deltaPosition.y = value.position.y - finger.position.y;
finger.position.x = value.position.x;
finger.position.y = value.position.y;
if (finger.deltaPosition.x != 0 || finger.deltaPosition.y != 0) {
// console.log("Swipe", key, Date.now());
if (this.listener) this.listener.onTouchSwipe(finger);
}
}
else {
// down start.
finger.deltaPosition.x = 0;
finger.deltaPosition.y = 0;
finger.position.x = value.position.x;
finger.position.y = value.position.y;
// console.log("Down", key);
if (this.listener) this.listener.onTouchDown(finger);
}
}
for (let key of this.oldTouches.keys()) {
if (!this.touches.has(key)) {
// up.
// console.log("Up", key);
if (this.listener) this.listener.onTouchUp(this.fingers.get(key));
}
}
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
1 个回复
Laya_Fred
赞同来自: