SceneObject
SceneObject is the base class for all SceneObjects in Scenes.
| Type | Name | Interface Description |
|---|---|---|
| Variables | enabled: boolean | • Function: The enabled status of the SceneObject itself. |
| Variables | layer: number | • Function: Layers can be used for selective rendering from cameras. |
| Variables | parent: SceneObject | null | • Function: The parent of the scene object. <br/> Changing the parent will modify the parent-relative position, <br/> scale and rotation but keep the world space position, rotation and scale the same. |
| Variables | scene: Scene | • Function: The scene to which this scene object belongs. |
| Functions | constructor() | |
| Functions | addComponent(type: string): Component | null | • Function: Adds a component of the specified type to the scene object. The type string should use the public APJS component name. Parameters • Returns The added component, or null if the component could not be added. |
| Functions | clone(): SceneObject | • Function: Clones the scene object. Returns A cloned scene object. |
| Functions | getChild(name: string): SceneObject | null | • Function: Finds a child by name and returns it. This lookup uses the child object name and returns the first matching child found by the underlying scene query. Parameters • Returns The found child SceneObject, or null if no child with matching name is found. |
| Functions | getChildren(): SceneObject[] | • Function: Gets all children scene objects of the scene object. Returns An array of child SceneObjects. |
| Functions | getComponent(type: string): Component | null | • Function: Gets a component of the specified type from the scene object. The type string should use the public APJS component name. Parameters • Returns The component of the specified type, or null if not found. |
| Functions | getComponents(type?: string): Component[] | • Function: Gets all components of the specified type from the scene object. When provided, the type string should use the public APJS component name. Parameters • Returns An array of components of the specified type or all components for no type input. |
| Functions | getComponentsRecursive(type: string): Component[] | • Function: Gets components of the specified type from this scene object and its descendants. The type string should use the public APJS component name. Parameters • Returns Matching components from the hierarchy. |
| Functions | getTransform(): Transform | • Function: Gets the transform component of the scene object. Returns The transform component. |
| Functions | isEnabledInHierarchy(): boolean | • Function: Gets whether the scene object is enabled in the hierarchy. This property determines if the scene object and its components are visible and active. Returns True if the scene object is enabled in the hierarchy, false otherwise. |
| Functions | removeComponent(comp: Component): boolean | • Function: Removes the specified component instance from the scene object. Parameters • Returns True if the component was successfully removed, false otherwise. |
| Functions | setEnabledInHierarchy(enabled: boolean): void | • Function: Sets whether the scene object is enabled in the hierarchy. Parameters • |
Examples
constructor()
let obj = new APJS.SceneObject();
Use Case
Example 1 — Object pool using clone() + setEnabledInHierarchy. Pre-spawn pool of clones from a template, acquire/release with show/hide.
@component()
export class ObjectPool extends APJS.BasicScriptComponent {
@serializeProperty
template!: APJS.SceneObject;
private pool: APJS.SceneObject[] = [];
private initialized = false;
private readonly POOL_SIZE = 10;
onUpdate(dt: number): void {
if (!this.initialized && this.template) {
this.initialized = true;
// Pre-create pool of hidden clones
for (let i = 0; i < this.POOL_SIZE; i++) {
const obj = this.template.clone();
obj.setEnabledInHierarchy(false);
this.pool.push(obj);
}
// Hide the template too
this.template.setEnabledInHierarchy(false);
}
}
acquire(x: number, y: number): APJS.SceneObject | undefined {
if (this.pool.length === 0) return undefined;
const obj = this.pool.pop()!;
obj.setEnabledInHierarchy(true);
const st = obj.getComponent("ScreenTransform") as APJS.ScreenTransform;
if (st) st.anchoredPosition = new APJS.Vector2f(x, y);
return obj;
}
release(obj: APJS.SceneObject): void {
obj.setEnabledInHierarchy(false);
this.pool.push(obj);
}
}
Example 2 — Audio manager with BGM loop + multiple SFX channels — demonstrates the BGM+SFX pattern with separate Audio Player objects
@component()
export class MultiSfxManager extends APJS.BasicScriptComponent {
@serializeProperty
bgmPlayer!: APJS.SceneObject;
@serializeProperty
clickPlayer!: APJS.SceneObject;
@serializeProperty
successPlayer!: APJS.SceneObject;
@serializeProperty
failPlayer!: APJS.SceneObject;
private bgm!: APJS.AudioComponent;
private clickSfx!: APJS.AudioComponent;
private successSfx!: APJS.AudioComponent;
private failSfx!: APJS.AudioComponent;
private initialized = false;
onUpdate(dt: number): void {
if (!this.initialized && this.bgmPlayer) {
this.bgm = this.bgmPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.clickSfx = this.clickPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.successSfx = this.successPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
this.failSfx = this.failPlayer.getComponent("AudioComponent") as APJS.AudioComponent;
if (!this.bgm || !this.clickSfx) return;
// Start BGM (playMode set to Infinity via DSL)
this.bgm.volume = 40;
this.bgm.play();
this.initialized = true;
}
}
playClick(): void {
this.clickSfx.stop();
this.clickSfx.loopCount = 1;
this.clickSfx.volume = 100;
this.clickSfx.play();
}
playSuccess(): void {
this.successSfx.stop();
this.successSfx.loopCount = 1;
this.successSfx.volume = 100;
this.successSfx.play();
}
playFail(): void {
this.failSfx.stop();
this.failSfx.loopCount = 1;
this.failSfx.volume = 100;
this.failSfx.play();
}
}