Correct syntax to assign layer to sprite via script?

I can move my pictures around with

sc._mySprite.move(0, 0);

but i am trying to assign layers to them but I don't know the correct way to write it.
I tried:
sc._mySprite.layer(1);
sc._mySprite.layer = 1;

does anyone know the correct format to write this? Any help is appreciated!
The first thing you wrote will cause an error, because it's trying to call a function called "layer" on mySprite and pass in 1 as an argument. But the sprite doesn't have a layer function, so it can't be called.

The second thing you wrote will work; it will create a property on the sprite called "layer" and give it a value of 1. But it won't do anything further unless you make it do something.
The first thing you wrote will cause an error, because it's trying to call a function called "layer" on mySprite and pass in 1 as an argument. But the sprite doesn't have a layer function, so it can't be called.

The second thing you wrote will work; it will create a property on the sprite called "layer" and give it a value of 1. But it won't do anything further unless you make it do something.
I see.

Let me give you the full script, maybe that will provide a bit more clarity.


const dir = $gameVariables.value(2311);

const filename = $gameVariables.value(2332);

const url = dir + "/" + Utils.encodeURI(filename) + ".jpg";

const sc = SceneManager._scene;

if (!sc._mySpriteC)

sc.addChild(sc._mySpriteC = new Sprite());

sc._mySpriteC.bitmap = ImageManager.loadBitmapFromUrl(url);

sc._mySprite.move(960, 0);


All of this works fine to load my jpg and move it where I want it. But my pngs which are loaded by the games default eventing are all placed behind the jpg.
So I wanted to assign a layer to the jpg script to place it behind the game's default pngs (which are using layers 50-60 at the moment).

I'm not sure how to write this to accomplish the desired result.
When you say "layer" are you referring to the picture IDs?
When you say "layer" are you referring to the picture IDs?
I think so.
Like the "Z" layer that is used to determine whether a picture appears in front of or behind another picture.
the engine doesn't use layers at all, the pictures are simply drawn in the order of the ID's - which will look like layers or Z-coordinates if you don't know the internals.
That means there is no property "layer" by default, so as explained above creating it or assigning numbers to it will do nothing.

instead you need to find the code responsible for drawing and updating your sprites and look at the order where and when each on is drawn. then take that order or id into account when creating the sprite.
the engine doesn't use layers at all, the pictures are simply drawn in the order of the ID's - which will look like layers or Z-coordinates if you don't know the internals.
That means there is no property "layer" by default, so as explained above creating it or assigning numbers to it will do nothing.

instead you need to find the code responsible for drawing and updating your sprites and look at the order where and when each on is drawn. then take that order or id into account when creating the sprite.

Thank you both for explaining! Given this new and better understanding I was able to fix my issue by reordering the scripts that called the pictures and tweaking them somewhat. All this time I thought I was dealing with layers. Knowing that the engine just draws the bitmaps for the sprites in order of the scriptcall request was priceless information! Thanks!

(Thread can be closed)
Thank you both for explaining! Given this new and better understanding I was able to fix my issue by reordering the scripts that called the pictures and tweaking them somewhat. All this time I thought I was dealing with layers. Knowing that the engine just draws the bitmaps for the sprites in order of the scriptcall request was priceless information! Thanks!

(Thread can be closed)
As a piece of additional information, what you *can* do in an array of sprites is set its "sortableChildren" property to true, then change the zIndex of the sprite to change where it's drawn.
This thread is closed due to being indicated as solved. Next time, be sure to use the report button so we can find it~ If you need this thread reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/correct-syntax-to-assign-layer-to-sprite-via-script.177600/