diff --git a/cocos/input/input.ts b/cocos/input/input.ts index ae2b8e368cc..742ed725cd2 100644 --- a/cocos/input/input.ts +++ b/cocos/input/input.ts @@ -251,9 +251,9 @@ export class Input { * @param clone - Whether to clone touch object * @returns */ - public getTouch (touchID: number, clone?: boolean): Touch | undefined { + public getTouch (touchID: number): Touch | undefined { const touch = touchManager._touchMap.get(touchID); - return touch ? (clone === false ? touch : touchManager._cloneTouch(touch)) : undefined; + return touch; } /** @@ -263,8 +263,8 @@ export class Input { * 获取当前 所有touch对象 的数组。 * @param clone - Whether to clone touch object */ - public getAllTouches (clone?: boolean): Touch[] { - return touchManager.getAllTouches(clone); + public getAllTouches (): Touch[] { + return touchManager.getAllTouches(); } /** diff --git a/cocos/input/types/touch.ts b/cocos/input/types/touch.ts index 1be58d480e2..2e0cc238702 100644 --- a/cocos/input/types/touch.ts +++ b/cocos/input/types/touch.ts @@ -199,7 +199,7 @@ export class Touch { _vec2.set(this._point); _vec2.subtract(this._prevPoint); - out.set(cclegacy.view.getScaleX(), cclegacy.view.getScaleY()); + out.set(cclegacy.view.getScaleX() as number, cclegacy.view.getScaleY() as number); Vec2.divide(out, _vec2, out); return out; } @@ -261,7 +261,7 @@ export class Touch { * @param x - x position of the touch point * @param y - y position of the touch point */ - public setTouchInfo (id = 0, x?: number, y?: number): void { + public setTouchInfo (id: number = 0, x: number = 0, y: number = 0): void { this._prevPoint = this._point; this._point = new Vec2(x || 0, y || 0); this._id = id; @@ -321,6 +321,21 @@ export class Touch { } this._lastModified = cclegacy.game.frameStartTime; } + + /** + * @zh Touch 对象的原始数据不应该被修改。如果你需要这么做,最好克隆一个新的对象。 + * @en The original Touch object shouldn't be modified. If you need to, it's better to clone a new one. + */ + public clone (): Touch { + const touchID = this.getID(); + this.getStartLocation(_vec2); + const clonedTouch = new Touch(_vec2.x, _vec2.y, touchID); + this.getLocation(_vec2); + clonedTouch.setPoint(_vec2.x, _vec2.y); + this.getPreviousLocation(_vec2); + clonedTouch.setPrevPoint(_vec2); + return clonedTouch; + } } cclegacy.Touch = Touch; diff --git a/pal/input/touch-manager.ts b/pal/input/touch-manager.ts index d237b274c81..aadaf422997 100644 --- a/pal/input/touch-manager.ts +++ b/pal/input/touch-manager.ts @@ -32,6 +32,7 @@ const tempVec2 = new Vec2(); class TouchManager { /** * A map from touch ID to touch object. + * @engineInternal */ public _touchMap: Map; private readonly _maxTouches = 8; @@ -45,7 +46,7 @@ class TouchManager { * @param touch * @returns */ - public _cloneTouch (touch: Touch): Touch { + private _cloneTouch (touch: Touch): Touch { const touchID = touch.getID(); touch.getStartLocation(tempVec2); const clonedTouch = new Touch(tempVec2.x, tempVec2.y, touchID); @@ -100,25 +101,25 @@ class TouchManager { * @param touchID * @returns */ - public getTouch (touchID: number, x: number, y: number, clone?: boolean): Touch | undefined { + public getTouch (touchID: number, x: number, y: number): Touch | undefined { let touch = this._touchMap.get(touchID); if (!touch) { touch = this._createTouch(touchID, x, y); } else { this._updateTouch(touch, x, y); } - return touch ? (clone === false ? touch : this._cloneTouch(touch)) : undefined; + return touch; } /** * Get all the current touches objects. * @returns */ - public getAllTouches (clone?: boolean): Touch[] { + public getAllTouches (): Touch[] { const touches: Touch[] = []; this._touchMap.forEach((touch) => { if (touch) { - touches.push(clone === false ? touch : this._cloneTouch(touch)); + touches.push(touch); } }); return touches;