MV Destroy child array in reverse order

Hello, in my game you drawing many time in using this script:
JavaScript:
const s = SceneManager._scene._spriteset; 
line = new PIXI.Graphics();
line.name = 'line'; s.addChild(line);
line.lineStyle(1,0x0000ff);
line.moveTo(100,100);
line.lineTo(200,200);

For delete them I use:
JavaScript:
SceneManager._scene._spriteset.getChildByName('line').destroy()

But it's not convenient for me because it's only destroy them from the first to the last.
For know I'm trying to use the script below to delete them in the reverse order.
JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children
for (let i = line.length - 1; i >= 0; i--) 
SceneManager._scene._spriteset.getChildAt(i - 0).destroy()

But each time I have i = 0, I have a black screen because all my scene is destroy.
How I do for it destroy only my children 'line' from the last to the first?
This doesn't directly answer your question, though it could.

Why destroy()?

Ideally, you're drawing onto a single bitmap and managing your lines with an object or array of coords which you can then reference using clearRect() to delete individual line sections.

If you absolutely must draw the lines as fully individual elements, maybe you'd instead create or build up a fixed pool of sprites whose bitmaps you can clear and return to the pool for future use.

Rather than presumably constantly using destroy().

Edit: also, this loop:

JavaScript:
SceneManager._scene._spriteset.getChildAt(i - 0).destroy()

Is nuking the entire spriteset.

The spriteset has more children than just your lines.
>= 0 is higher or equal to 0, > 0 is higher than 0, so it start at 1.

so I think you want it the other way around.

JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children
for (let i = 0; i < line.length - 1; i--)
SceneManager._scene._spriteset.getChildAt(i - 0).destroy()

but there are also scripts that checks the back to slice it from the back 😉

another way probably what can work is:

JavaScript:
const line = s.children
          line.reverse()  // or reversed()

you might need that reverse part after the length, so it goes backwards.
as you delete the 1st index in reverse order.
>= 0 is higher or equal to 0, > 0 is higher than 0, so it start at 1.

so I think you want it the other way around.

JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children
for (let i = 0; i < line.length - 1; i--)
SceneManager._scene._spriteset.getChildAt(i - 0).destroy()

but there are also scripts that checks the back to slice it from the back 😉

another way probably what can work is:

JavaScript:
const line = s.children
          line.reverse()  // or reversed()

you might need that reverse part after the length, so it goes backwards.
as you delete the 1st index in reverse order.
When I use the script below I have an error "getChildAt: Index(-1) does not exist"
JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children
for (let i = 0; i < line.length - 1; i--)
SceneManager._scene._spriteset.getChildAt(i - 0).destroy()

"line.reverse()" will not work because for "getChildAt(i - 0)" I plan to replace the 0 by a variable, example:
I have make 10 drawings but the seventh one isn't good -> set a variable to 3 to destroy the third drawing from the last.
It's important to be from the last to the first because it's easier to track last drawings, think of it as a backup system in case of mistake.
I think you can do the "getChildAt(0)" instead or 1 after reversed order.
as you might be able to to ++i as well.

there are a few methods to get the array index.
and some make array index for a new 1 to be empty, than push the
array in reversed order and than use that array instead.

I'm not sure how it works with pixi children if it is similar or not, but that can
do the trick otherwise to get the name or first it picks inside.

anotherway migh be naming "line_" + i so it has line 0 to line max.
but I don't know how it can recognise what is taken out already (based on variable).
I think you can do the "getChildAt(0)" instead or 1 after reversed order.
as you might be able to to ++i as well.

there are a few methods to get the array index.
and some make array index for a new 1 to be empty, than push the
array in reversed order and than use that array instead.

I'm not sure how it works with pixi children if it is similar or not, but that can
do the trick otherwise to get the name or first it picks inside.

anotherway migh be naming "line_" + i so it has line 0 to line max.
but I don't know how it can recognise what is taken out already (based on variable).
Thanks dude it's work, if I reverse one first time and then I reverse again after I destroy the child its will not interfere with array sort
JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children; line.reverse()
SceneManager._scene._spriteset.getChildAt(0).destroy()
line.reverse()

Then if I want to decide which array I want to destroy it's give:
◆Input Number:#Z, x digit
JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children; line.reverse()
$gameVariables.setValue(z, $gameVariables.value(z) - 1)
SceneManager._scene._spriteset.getChildAt($gameVariables.value(z)).destroy()
line.reverse()

But if I put a value egal to 0 or superior to the array length its crash. So I finally come to these scripts:
JavaScript:
const s = SceneManager._scene._spriteset;
line = new PIXI.Graphics();
line.name = 'line'; s.addChild(line);
line.lineStyle(size,color);
line.moveTo(x0,x0);
line.lineTo(x1,y1);
$gameVariables.setValue(crash, $gameVariables.value(crash) + 1)

◆Input Number:#Z, X digit
◆If:Script:($gameVariables.value(z) > 0) && ($gameVariables.value(z) <= $gameVariables.value(crash))
JavaScript:
const s = SceneManager._scene._spriteset;
const line = s.children; line.reverse()
$gameVariables.setValue(z, $gameVariables.value(z) - 1)
SceneManager._scene._spriteset.getChildAt($gameVariables.value(z)).destroy()
line.reverse()
$gameVariables.setValue(crash, $gameVariables.value(crash) - 1)

I was struggled about this since the last month, if someone is curious about the project in this thread there are here all the details https://forums.rpgmakerweb.com/index.php?threads/display-tiles-grid.175982/
If:Script:($gameVariables.value(z) > 0) && ($gameVariables.value(z) <= $gameVariables.value(crash))
this wont work but it might work as
JavaScript:
$gameVariables.value(z).length > -1
so if its NOT or empty, it does nothing (if using an else handler.
index starts with 0, so -1 for undefined/null.

see if that works 🙂
this wont work but it might work as
JavaScript:
$gameVariables.value(z).length > -1
so if its NOT or empty, it does nothing (if using an else handler.
index starts with 0, so -1 for undefined/null.

see if that works 🙂
No no I assure you I tested my method and its work. Then maybe you method work and is more elegant that mine but as much its work I don't care about the method used 🙂
Can you not just change

JavaScript:
const line = s.children

to

JavaScript:
const line = s.children.filter(child -> child.name == "line")

Original thread: https://forums.rpgmakerweb.com/threads/destroy-child-array-in-reverse-order.176500/