TouchData
The touch data is carried by touch event, record the info of one touch point.
| Type | Name | Interface Description |
|---|---|---|
| Variables | force: number | • Function: Get the force of the touch. Typically a normalized value between 0 and 1. |
| Variables | phase: TouchPhase | • Function: The touch phase of this touch. Returns The current phase of the touch as a TouchPhase enum value. |
| Variables | position: Vector2f | • Function: Gets the position of the touch in normalized screen coordinates. Coordinates use the range [0, 1]. Returns Normalized position of the touch as a Vector2f. Value range: x ∈ [0, 1], y ∈ [0, 1] (0 = top, 1 = bottom). |
| Variables | touchCount: number | • Function: Get the number of active touches. Returns The total number of active touches on the screen. |
| Variables | touchId: number | • Function: Get the unique identifier for this touch. Returns The unique ID of the touch, useful for tracking individual touches in multi-touch scenarios. |
| Functions | constructor() |
Examples
position: Vector2f
const touch = event.args[0] as APJS.TouchData;
const touchPos = new APJS.Vector2f(touch.position.x, 1.0 - touch.position.y);
const screenPos = touchPos.clone().multiply(new APJS.Vector2f(720, 1280));
constructor()
let obj = new APJS.TouchData();
Use Case
Example 1 — Tap screen to toggle object visibility on/off
@component()
export class TapToggleVisibility extends APJS.BasicScriptComponent {
private touchCallback: (event: APJS.IEvent) => void;
private isVisible: boolean = true;
onStart(): void {
this.touchCallback = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began) {
this.isVisible = !this.isVisible;
this.getSceneObject().enabled = this.isVisible;
}
};
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback, this);
}
onDestroy(): void {
if (this.touchCallback) {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback, this);
}
}
}
Example 2 — Toggle visibility of all children in a container object on tap
@component()
export class SceneChildrenToggle extends APJS.BasicScriptComponent {
@serializeProperty
private container: APJS.SceneObject;
private childrenVisible: boolean = true;
private onTouchEvent = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began) {
this.childrenVisible = !this.childrenVisible;
const children = this.container.getChildren();
for (const child of children) {
child.enabled = this.childrenVisible;
}
}
};
onStart(): void {
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.onTouchEvent);
}
onDestroy(): void {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.onTouchEvent);
}
}