BasicScriptComponent
Base class for script components attached to SceneObjects.
| Type | Name | Interface Description |
|---|---|---|
| Functions | getSceneObject(): SceneObject | • Function: Get the scene object this component is attached to. Returns The scene object this component is attached to. |
| Functions | onDestroy(): void | • Function: Called by engine when component is destroyed. |
| Functions | onDisable(): void | • Function: Called by engine when component itself is disabled. |
| Functions | onEnable(): void | • Function: Called by engine when component itself is enabled. |
| Functions | onEvent(event: IEvent): void | • Function: Called by the runtime when a global event is dispatched. Only pre-registered event types are delivered here ( Parameters • |
| Functions | onInit(): void | • Function: Called by engine when component is added to SceneObject, will be called even if component is disabled. |
| Functions | onLateUpdate(deltaTime: number): void | • Function: Called by engine every frame after all systems updated. Parameters • |
| Functions | onStart(): void | • Function: Called by engine when component is started, will not be called if component is disabled. |
| Functions | onUpdate(deltaTime: number): void | • Function: Called by engine every frame. Parameters • |
Examples
onEvent(event: IEvent): void
// Recommended: use GlobalEmitter for Touch / Record events
export class MyScript extends APJS.BasicScriptComponent {
onStart() {
const emitter = APJS.EventManager.getGlobalEmitter();
emitter.on(APJS.EventType.Touch, this.onTouch, this);
}
onDestroy() {
const emitter = APJS.EventManager.getGlobalEmitter();
emitter.off(APJS.EventType.Touch, this.onTouch, this);
}
private onTouch(event: APJS.IEvent) {
const touch = event.args[0] as APJS.TouchData;
}
}
Use Case
Example 1 — Continuously rotate object around Y axis at a constant speed
@component()
export class ContinuousRotation extends APJS.BasicScriptComponent {
private rotationSpeed: number = 90;
onUpdate(deltaTime: number): void {
const transform = this.getSceneObject().getTransform();
const euler = transform.localEulerAngles;
transform.localEulerAngles = new APJS.Vector3f(
euler.x,
euler.y + this.rotationSpeed * deltaTime,
euler.z
);
}
}
Example 2 — Object fades in and out continuously (opacity animation)
@component()
export class FadeInOut extends APJS.BasicScriptComponent {
private elapsed: number = 0;
private speed: number = 1;
onUpdate(deltaTime: number): void {
this.elapsed += deltaTime;
const alpha = (Math.sin(this.elapsed * this.speed * Math.PI * 2) + 1) / 2;
const image = this.getSceneObject().getComponent("Image") as APJS.Image;
if (image) {
const c = image.color;
image.color = new APJS.Color(c.r, c.g, c.b, alpha);
}
}
}