Skip to main content

IEvent

Represents an event.

TypeNameInterface Description
Variablesargs: any[]

Function: Arguments passed to the event. The payload shape depends on the event type: EventType.Touch uses [TouchData], pinch events use [IPinchInfo], collision events use [CollisionInfo] or [CollisionInfo2D].

Variablestype: number | UserEventType

Function: The type of the event, which can be either a number or a UserEventType.

Use Case

Example 1 — Tap to drop a ball/coin that bounces multiple times on a static floor — full Physics Matter resource flow (DSL: add_builtin_resource → set_resource_properties →…

@component()
export class TapToDropBouncy extends APJS.BasicScriptComponent {
private rb: APJS.RigidBody2D | null = null;
private collider: APJS.CircleCollider2D | null = null;
private touchCallback: ((event: APJS.IEvent) => void) | null = null;
private collisionCallback: ((event: APJS.IEvent) => void) | null = null;
private dropped = false;
private bounceCount = 0;

onStart(): void {
const obj = this.getSceneObject();
this.rb = obj.getComponent("RigidBody2D") as APJS.RigidBody2D;
this.collider = obj.getComponent("CircleCollider2D") as APJS.CircleCollider2D;
if (!this.rb || !this.collider) return;

// emitCollisionEvent is APJS-runtime-only — must set in onStart, not in DSL.
this.collider.emitCollisionEvent = true;

this.touchCallback = (event: APJS.IEvent) => {
const touch = event.args[0] as APJS.TouchData;
if (touch.phase === APJS.TouchPhase.Began && !this.dropped && this.rb) {
this.dropped = true;
this.rb.static = false; // gravity now applies
}
};
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback);

this.collisionCallback = (event: APJS.IEvent) => {
this.bounceCount++;
// Optional: stop tracking after 5 bounces, or play SFX, or update score, etc.
if (this.bounceCount === 1) console.log("[Bouncy] first impact");
};
const emitter = APJS.EventManager.getObjectEmitter(this.collider);
emitter.on(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
}

onDestroy(): void {
if (this.touchCallback) {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback);
}
if (this.collisionCallback && this.collider) {
APJS.EventManager.getObjectEmitter(this.collider).off(APJS.CollisionEvent2D.Enter, this.collisionCallback, this);
}
}
}

Example 2 — Tap screen to clone/spawn a copy of this object at the touch position

@component()
export class TapSpawnAtTouch extends APJS.BasicScriptComponent {
private touchCallback: (event: APJS.IEvent) => void;
private spawned: APJS.SceneObject[] = [];

// RecordStart: remove every spawned clone. See GameState §"RecordStart / RecordEnd Lifecycle".
private onRecordStart = (_event: APJS.IEvent) => {
const scene = this.getSceneObject().scene;
for (const s of this.spawned) {
scene.removeSceneObject(s);
}
this.spawned = [];
};

onStart(): void {
this.touchCallback = (event: APJS.IEvent) => {
const touchInfo = event.args[0] as APJS.TouchData;
if (touchInfo.phase === APJS.TouchPhase.Began) {
const clone = this.getSceneObject().clone();
const screenPos = new APJS.Vector2f(
touchInfo.position.x * 720,
(1.0 - touchInfo.position.y) * 1280
);
const st = clone.getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st) {
st.anchoredPosition = screenPos.clone().subtract(new APJS.Vector2f(360, 640));
}
this.spawned.push(clone);
}
};
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.Touch, this.touchCallback, this);
APJS.EventManager.getGlobalEmitter().on(APJS.EventType.RecordStart, this.onRecordStart);
}

onDestroy(): void {
if (this.touchCallback) {
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.Touch, this.touchCallback, this);
}
APJS.EventManager.getGlobalEmitter().off(APJS.EventType.RecordStart, this.onRecordStart);
}
}
Copyright © 2026 TikTok. All rights reserved.
About TikTokHelp CenterCareersContactLegalTerms of ServicePrivacy PolicyCookies