How to mount h2d.Layers the correct way?


What’s the correct way to work with Layers - how it was intended to be used?

My Current approach is this:

// 1. Created layers
var layers = new Layers(camera);

// 2. Created graphic utilities without mounting them
var g = new h2d.Graphics();
var tileGroup = new h2d.TileGroup(tileImg);

// 3. Adding objects to layers mounts them anyways.
layers.add( g, 1);
layers.add( tileGroup, 0);

There are many ways to use it with the same results. How was it intended to be integrated into a scene or how do you use it?

Am I using it incorrectly right now?


I’m deciding to answer this myself since I’m now doing it a different way and it feels right.

Since step two doesn’t actually rely on step 1, you don’t need step one. And therefore you can keep all of the “layers” code tucked neatly at the end. Like this:

// Step 1
var player = new Object();

// Step 2
var layers = new Layers(s2d);
layers.add( player, layerPieces );


Layers are just an utility to make it easier to separate things : instead of creating a single Object container for each “layer”, you can access them by index.

You can always choose to use the regular scene objects tree directly instead if that works better for you.