Showing code for

Spawn objects at runtime

Create enemies, coins and list rows from templates while the game runs.

Every template in the zStudio Templates panel can be created from code. A spawned object is not added to the screen until you add it.

const coin = ZSceneStack.spawn('Coin') as ZContainer;

coin.x = 300;
coin.y = 150;
scene.sceneStage.addChild(coin);

// Remove it when finished
coin.removeFromParent();
coin.destroy({ children: true });

ZSceneStack.spawn() looks through every scene on the stack, newest first. To use one specific scene, call scene.spawn().

Positions are in scene units, the same coordinates you see in zStudio, so scene.sceneWidth and scene.sceneHeight give you the size of the stage for the current orientation.

Spawned objects and resizing

A spawned object has no stored layout, so resizing never moves it. You own its position. The scene's scaling still applies, because it sits inside the stage.

If you need to place it differently in each orientation, do it in your own resize code.