Display Tiles Grid

I am going to take a guess in that you are also creating a new container each time to that scene. So you will need to loop through all containers as well (assuming you named them 'container'):
JavaScript:
const s = SceneManager._scene._spriteset.children;
for (let i = 0; i < s.length; i++) {
  if (s[i].name === 'container') {
    for (let j = 0; i < s.children.length; j++) {
      const c = s[i].children[j];
      if (c.name === 'house') c.clearRect(0, 0, 200, 200);
    }
  }
My apologies if this is not clear and if there is a language barrier - I am not sure so I am assuming based on how and what you are typing:
Hope this helps! Anything else will have to come from experimenting because I don't know your true goals and what you are after and everything that you do.

I don't know what else to say - it's working here so you are going to have to debug it yourself. Please use the code that iterates through both the containers and sprites/bitmaps and not the code that only iterates through one container.

I can only do so much unfortunately and your success is, again, based on your ability to navigate through this. And, you have done very well, just so you know.


It's just a proposition but until now you have posted a way to clean 'house' child, but wouldn't be more efficient to destroy 'container' child instead?
Yes! To answer that:
It becomes harder to deal with because you can't always keep track of what you added and you add more guesswork to your workload. Destroy() might be better if you know you aren't going to use that bitmap anymore.

From an earlier post about pixi graphics which also applies:
Currently, the way I showed you, when I destroy the child holding the other children, it erases everything all at once, not one at a time.
That means that if you destroy the parent, all children are destroyed as well.

This is assuming you are destroying one container that encapsulates every new bitmap. You asked about removing them in a certain order so that is what I offered you. My proposition was also assuming that you were also going to reference only one bitmap and continue to draw on that same one, not create any new containers with their own bitmaps.

Once you run out of containers or bitmaps, using destroy will result in an error if nothing exists.

One last thing: can you confirm if I destroy a child then the remaning arrays will rearrange themself?
Yes. Please test as well to confirm.
My apologies if this is not clear and if there is a language barrier - I am not sure so I am assuming based on how and what you are typing:


I don't know what else to say - it's working here so you are going to have to debug it yourself. Please use the code that iterates through both the containers and sprites/bitmaps and not the code that only iterates through one container.

I can only do so much unfortunately and your success is, again, based on your ability to navigate through this. And, you have done very well, just so you know.



Yes! To answer that:


From an earlier post about pixi graphics which also applies:

That means that if you destroy the parent, all children are destroyed as well.

This is assuming you are destroying one container that encapsulates every new bitmap. You asked about removing them in a certain order so that is what I offered you. My proposition was also assuming that you were also going to reference only one bitmap and continue to draw on that same one, not create any new containers with their own bitmaps.

Once you run out of containers or bitmaps, using destroy will result in an error if nothing exists.


Yes. Please test as well to confirm.
JavaScript:
const s = SceneManager._scene._spriteset.children;
const w = 500, h = 500;
const container = new PIXI.Container(); container.name = 'container'; s.addChild(container);
const child = s[s.length - 1].children;
const bitmap = child[child.length - 1].bitmap;
const ctx = bitmap._context; 
const r = PIXI.RenderTexture.create(w, h);
Graphics._renderer.render(container, r);

ctx drawing ....

r.destroy({destroyBase: true});
bitmap._setDirty();
const spr = new Sprite(bitmap); spr.name = 'house'; container.addChild(spr);
It doesn't work, I will repeat myself again but because of "SceneManager._scene._spriteset.children;"
I have now "s.addChild is not a function".
With "const bitmap = child[child.length - 1].bitmap;" instead of "const bitmap = new Bitmap(w, h)"
I have "cannot read property '_context' of undefined"

JavaScript:
const s = SceneManager._scene._spriteset.children;
const child = s[s.length - 1].children;
child[child.length].destroy();
And then I have "cannot read property 'destroy' of undefined"
Ok I wil resume about the project:

I tried to draw line which correspond to tiles border. At first I used this but I had some issue to destroy more that 1 with : SceneManager._scene._spriteset.getChildByName('graph').destroy()
JavaScript:
const s = SceneManager._scene._spriteset;
const g = new PIXI.Graphics()
 g.name = 'graph'; s.addChild(g);
 const b = new PIXI.Graphics()
 .lineStyle(1, 0x0000ff)
 .moveTo(100,100))
 .lineTo(200,200)
b.name = 'bars';  g.addChild(b);

Next I tried with canvas to finally go to a hybrid version with PIXI and canvas
JavaScript:
const c = document.createElement('canvas');
const ctx = c.getContext('2d');
c.setAttribute('width', 300);
c.setAttribute('height', 200);

ctx.lineWidth = 1;
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(100, 200);
ctx.lineTo(100, 200);
ctx.closePath();
ctx.stroke();

const spr = new PIXI.Sprite.from(c);
SceneManager._scene._spriteset.addChild(spr);
But with this method I could only display 1 drawing at once. With the Pixi method I could draw how many I wanted but couldn't delete more that 1 (the first I had drawed), but with the canvas way I could use clearRect to clear many at once: problem I couldn't draw more that 1 time.

So I looked for a way to draw PIXI elements and then convert them into canvas element to clear them.
JavaScript:
const s = SceneManager._scene._spriteset;
const w = 500, h = 500;
const container = new PIXI.Container();
s.name = 'container'; s.addChild(container);

const bitmap = new Bitmap(w, h);
const ctx = bitmap._context;
const r = PIXI.RenderTexture.create(w, h);
Graphics._renderer.render(container, r);
ctx.lineWidth = 1; ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(200, 200);
ctx.closePath(); ctx.stroke();
r.destroy({destroyBase: true});
bitmap._setDirty();

const spr = new Sprite(bitmap);
spr.name = 'house'; container.addChild(spr);

To clear them I used this:
JavaScript:
const s = SceneManager._scene._spriteset.getChildByName('container');
for (const child of s.children) {
  child.bitmap.clearRect(0, 0, 200, 200);
}
But an other problem to add: each time I used the drawing script, it created each time a new array so whatever I tried to clear it work but it stucked at the first array.
Then I looked there was a way to merge/flat all the drawing to 1 layer (to not have to manage array) but it was outside of my knowledge.

So finally, I'm gone back to the square one and with my first script I have found to make it simpler:
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);

With this script I can do any number of drawing, and if I use:
JavaScript:
SceneManager._scene._spriteset.getChildByName('line').destroy()
All drawing are erase one by one (from the first to the last)

So next I'm will need to looking about the length of array to clear them in the reverse order. But I don't manage to use "getChildIndex" or "getChildAt" and do apply it "length -1" :
JavaScript:
SceneManager._scene._spriteset.getChildIndex().destroy()
SceneManager._scene._spriteset.getChildAt().destroy()



EDIT:

It finally done thanks to guy from here:
https://forums.rpgmakerweb.com/index.php?threads/destroy-child-array-in-reverse-order.176500/


Here the script:
JavaScript:
const s = SceneManager._scene._spriteset;
line = new PIXI.Graphics();
line.name = 'line'; s.addChild(line);
line.lineStyle(size, $gameVariables.value(color)[num]);
line.moveTo(x0,y0);
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)

And for clear all:
◆Label:loop
◆If:Script:$gameVariables.value(crash) > 0
JavaScript:
SceneManager._scene._spriteset.getChildByName('line').destroy()
$gameVariables.setValue(crash, $gameVariables.value(crash) - 1)
◆If:Script:$gameVariables.value(crash) > 0
◆Jump to Label:loop

Original thread: https://forums.rpgmakerweb.com/threads/display-tiles-grid.175982/