Scene
In scripts, access the current scene from the mounted scene object.
| Type | Name | Interface Description |
|---|---|---|
| Functions | constructor() | |
| Functions | commitCommandBuffer(buffer: CommandBuffer): void | • Function: commitCommandBuffer Parameters • |
| Functions | createSceneObject(name: string): SceneObject | • Function: Creates and adds a scene object to this scene. Parameters • Returns The created SceneObject. |
| Functions | findSceneObject(name: string, root?: SceneObject): SceneObject | null | • Function: Finds a scene object by name from a scene object tree or subtree in the scene. If the root is not provided, the search is performed from the root of the scene. If multiple scene objects share the same name, only the first matching object is returned. So unique names are recommended when using this method, or ensure that the name is unique within the subtree. Parameters • • Returns The found scene object, or null if not found. |
| Functions | getAllSceneObjects(): SceneObject[] | • Function: get all the sceneObject in the Scene. |
| Functions | getRootSceneObjects(): SceneObject[] | • Function: get all the root scene objects in the Scene. |
| Functions | postResetEvent(): void | • Function: Post a reset event which will take effects next frame. The reset result is the same as "reset on record" feature. This feature works only if the "Reset on Record" setting is "on," which is the default setting. |
| Functions | removeSceneObject(obj: SceneObject): boolean | • Function: Removes a scene object from the scene. <br/>Returns false if the object does not belong to the scene or is invalid. Parameters • Returns Returns true if the object was removed successfully; otherwise false. |
Examples
constructor()
let obj = new APJS.Scene();
createSceneObject(name: string): SceneObject
const scene = this.getSceneObject().scene;
const newObj = scene.createSceneObject('newObj');
findSceneObject(name: string, root?: SceneObject): SceneObject | null
let foundObj = this.getSceneObject().scene.findSceneObject('newObj');
if (foundObj) {
this.getSceneObject().scene.removeSceneObject(foundObj);
}
getAllSceneObjects(): SceneObject[]
var sceneObjects = this.getSceneObject().scene.getAllSceneObjects();
sceneObjects.forEach(obj => {
// TODO
})
postResetEvent(): void
export class NewScriptComponent extends APJS.BasicScriptComponent {
private currentScore: number;
onStart() {
// the score should be reset when record start
let callback = (event:IEvent) => {
this.currentScore = 0;
...
}
const globalEmitter = APJS.EventManager.getGlobalEmitter();
globalEmitter.on(APJS.EventType.RecordStart, callback);
}
onUpdate(deltaTime: number) {
// When the score exceeds the limit, a manual reset can be triggered, which will activate all reset logic for the record event.
if (this.currentScore > 100) {
this.getSceneObject().scene.postResetEvent();
}
}
}
removeSceneObject(obj: SceneObject): boolean
let foundObj = this.getSceneObject().scene.findSceneObject('newObj');
if (foundObj) {
this.getSceneObject().scene.removeSceneObject(foundObj);
}
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();
}
}