MZ Is there a faster, more efficient way to add multiple of the same child?

Sorry that I constantly turn to you guys for help, but I am in a bit of a situation right now.

I am currently attempting to make multiple of the same child but each clone is somewhere near or around the original. Almost like tessellated hexagons, except instead of drawing primitive, I’m using bitmaps based on sprites.

I could use help on cleaning up my code. Pun sort of intended (this is a fossil fighters style game, after all).

Plus I’m trying to repurpose the Dragon Bones demo (with the inclusion of a dig site to explore) to serve as an entry for RPG Mania game jam over at itch.io. I’m desperately trying to get my game concept to be a working, playable demo.

Private message me if you would like to help out.

Anyways, let me include some code to show you how exactly I’m pulling this off.

For every sprite featured in the excavating system, I am formatting it as follows:

JavaScript:
this._customSprite = new Sprite();
this._customSprite.bitmap = ImageManager.loadCustomBitmap("customImage");
this._customSprite.move(x, y, width, height);
this._baseSprite.addChild(this._customSprite);

I fear that if I create more of the same child, it will much lead to performance issues, I do not plan to deal with. Especially when trying to hit the ground with a hammer in the game to break most of the surface level off.

If anyone can help me, please do so.
I'm eyeballing it based on another concept I've seen elsewhere here, but couldn't you *draw* whatever you need into a bitmap and ultimately add that bitmap as a child?
like, it wouldn't be an array of children, it would be one bitmap object.

operating on that bitmap afterwards might be trickier than deleting one child from an array, but if general load is the issue, you can remove that by turning the lot of objects into one object: all things being bitmaps, just move one bitmap to another by drawing, through the bitmap object's own methods.
Anyways, let me include some code to show you how exactly I’m puking this off.
It's not relevant to your question, but...that sounds like some gross code.
It's not relevant to your question, but...that sounds like some gross code.
Sorry, autocorrect on my phone.
I'm eyeballing it based on another concept I've seen elsewhere here, but couldn't you *draw* whatever you need into a bitmap and ultimately add that bitmap as a child?
like, it wouldn't be an array of children, it would be one bitmap object.

operating on that bitmap afterwards might be trickier than deleting one child from an array, but if general load is the issue, you can remove that by turning the lot of objects into one object: all things being bitmaps, just move one bitmap to another by drawing, through the bitmap object's own methods.
I admit it that this is rather confusing, so how exactly would I do that?
I admit it that this is rather confusing, so how exactly would I do that?
Does this involve using a for loop with i and j? I can do that, but I can’t seem to figure out how to find a specific child based on the TouchInput’s current position on the screen. Thus changing the color and mask of said child to respective layer.
i dont know if this will help, but

JavaScript:
var b1 = 4;
var v1 = 2;
for (let i = 0; i < b1; i++) // b1 = the amount of times this will loop
    {
     this.removeChild(this.getChildAt(v)); // v1 = the index where theimage is located, careful with this
    }

 this.removeChild(this._myimg); //can also be used to remove images but cant be used in a loop as is
//================================================

//for adding using a loop you can

for (let j = 0; j < 8; j++) {
    this.image = new Sprite(ImageManager.loadTitle2("myimage"));
    this.image.y = 0;
    this.image.x = 0;
    this.image.anchor.x = 0.5;
    this.image.anchor.y = 0.5;
    this.addChild(this.image);   
    }

// then to access the individual children you can use
this.getChildAt(i); //i = a number for the index of the specific child.
// you can do more with this for example
this.getChildAt(5).x;
this.getChildAt(5).y;
this.getChildAt(5).getframe;
this.getChildAt(5).rotate;

//and lots more

i hope this helps some.
dont give up ok?
theres always someone willing to help~
__〆( ̄ー ̄ )
Hi there!

I'm not sure if I understand you correctly, but doing it like this for every sprite:

For every sprite featured in the excavating system, I am formatting it as follows:

JavaScript:
this._customSprite = new Sprite();
this._customSprite.bitmap = ImageManager.loadCustomBitmap("customImage");
this._customSprite.move(x, y, width, height);
this._baseSprite.addChild(this._customSprite);

Does this not mean that instead of creating a new one, you are always overwriting the previous _customSprite you created?
Maybe you can create them like an array?

JavaScript:
const customSprite = new Sprite();
customSprite.bitmap = ImageManager.loadCustomBitmap("customImage");
customSprite.move(x, y, width, height);
this._baseSprite.addChild(customSprite);
this._customSpriteList.push(customSprite)
Hi there!

I'm not sure if I understand you correctly, but doing it like this for every sprite:



Does this not mean that instead of creating a new one, you are always overwriting the previous _customSprite you created?
Maybe you can create them like an array?

JavaScript:
const customSprite = new Sprite();
customSprite.bitmap = ImageManager.loadCustomBitmap("customImage");
customSprite.move(x, y, width, height);
this._baseSprite.addChild(customSprite);
this._customSpriteList.push(customSprite)
whaaa? you can do this??? how??? this would be so much more convenient than what I've been doing?? but how does this even work?? I thought you'd need to make an objkt or sumthin to put that... whaaA?? also how would this even be implemented? and or accessed or removed?
Hi there!

I'm not sure if I understand you correctly,

Allow me to explain what I'm trying to do in proper detail:
I am attempting to create an artificial hexagon-based tilemap using bitmaps. Below is an image of what I have so far is doing, and based on the answers I was given:

1655543141791.png


And here is the code I used to get this far (plus how I used for loops):

JavaScript:
Spriteset_Excavating.prototype.createEmptyTiles = function(){
  var tileSize = 32;
  this._emptyTileList = [];
  for(let i = 1; i < 24; i++){
    for(let j = 2; j < 16; j++){
      var endurance = getDurability(); // returns the durability of the surface above the hidden fossil
      endurance *= 100; // and multiply the durability by this value
      this._emptyTile = new Sprite();
      this._emptyTile.bitmap = ImageManager.loadHole("Empty");
      this._emptyTile.move(i*tileSize, j*tileSize, Graphics.width, Graphics.height);
      this._emptyTileList.toughness = Math.floor((Math.random() * endurance) + endurance / 2);
      this._rock.addChild(this._emptyTile);
      this._emptyTileList.push(this._emptyTile);
    }
  }
};
//This is the function that creates a hole image on hammer's impact.
Spriteset_Excavating.prototype.createHoles = function(){
  var hammerPower = $gameVariables.value(10);
  var endurance = getDurability();
  if(hammerEquipped){ //for each time the mouse clicks on the screen while the hammer tool is equipped, it creates a small tessellating series of hexagon bitmaps
        for(let i = -hammerPower/2.5; i < hammerPower/2.5; i++){
          for(let j = -hammerPower/2; j < hammerPower/2; j++){
          let moveModX = TouchInput.x + 32 * i;
          let moveModY = TouchInput.y + 32 * j;
          this._earthA = new Sprite(ImageManager.loadHole("RockHole")); // the brown hexagon bitmap.
          this._earthA.x += moveModX;
          this._earthA.y += moveModY;
          this._rock.addChild(this._earthA);
    }
  }
}
};

With this, I am currently finding answers to how I can get the specific child of a parent (in this case is the this._rock property), but as seen in the image above they just overlap based on touch input instead of the positions of the hexagon currently being hovered over by the mouse. Tbh, I don't know how to make them tessellate like a proper tilemap.
Allow me to explain what I'm trying to do in proper detail:
I am attempting to create an artificial hexagon-based tilemap using bitmaps. Below is an image of what I have so far is doing, and based on the answers I was given:

View attachment 229688

And here is the code I used to get this far (plus how I used for loops):

JavaScript:
Spriteset_Excavating.prototype.createEmptyTiles = function(){
  var tileSize = 32;
  this._emptyTileList = [];
  for(let i = 1; i < 24; i++){
    for(let j = 2; j < 16; j++){
      var endurance = getDurability(); // returns the durability of the surface above the hidden fossil
      endurance *= 100; // and multiply the durability by this value
      this._emptyTile = new Sprite();
      this._emptyTile.bitmap = ImageManager.loadHole("Empty");
      this._emptyTile.move(i*tileSize, j*tileSize, Graphics.width, Graphics.height);
      this._emptyTileList.toughness = Math.floor((Math.random() * endurance) + endurance / 2);
      this._rock.addChild(this._emptyTile);
      this._emptyTileList.push(this._emptyTile);
    }
  }
};
//This is the function that creates a hole image on hammer's impact.
Spriteset_Excavating.prototype.createHoles = function(){
  var hammerPower = $gameVariables.value(10);
  var endurance = getDurability();
  if(hammerEquipped){ //for each time the mouse clicks on the screen while the hammer tool is equipped, it creates a small tessellating series of hexagon bitmaps
        for(let i = -hammerPower/2.5; i < hammerPower/2.5; i++){
          for(let j = -hammerPower/2; j < hammerPower/2; j++){
          let moveModX = TouchInput.x + 32 * i;
          let moveModY = TouchInput.y + 32 * j;
          this._earthA = new Sprite(ImageManager.loadHole("RockHole")); // the brown hexagon bitmap.
          this._earthA.x += moveModX;
          this._earthA.y += moveModY;
          this._rock.addChild(this._earthA);
    }
  }
}
};

With this, I am currently finding answers to how I can get the specific child of a parent (in this case is the this._rock property), but as seen in the image above they just overlap based on touch input instead of the positions of the hexagon currently being hovered over by the mouse. Tbh, I don't know how to make them tessellate like a proper tilemap.
Hi!

As I said, I think the problem is happening because you are overwriting the same sprite on every loop.
Meaning that this._emptySprite is always referencing the same object on memory.
Maybe you can read this for you to understand it better:

You need to create a new one on each loop iteration.
Both for Empty Tile Sprite and for the Earth A sprite. So they will be indeed different sprites.

Both "this._rock" and this._emptyTileList are filled with just the same sprite, over and over again.
You need to take a different approach as I showed to you before.
If you can test that, and the error changes/persists, we can try having another idea of the problem.

whaaa? you can do this??? how??? this would be so much more convenient than what I've been doing?? but how does this even work?? I thought you'd need to make an objkt or sumthin to put that... whaaA?? also how would this even be implemented? and or accessed or removed?
- You can access them by their array position. In that case, I showed the const created will be only locally inside the function. So for you to be able to access that sprite outside that function, is nice if you create an array to store each one.

I'm sorry both of you, but English is not my native language. So maybe I'm not the best at explaining that technical stuff.
With the answers given I was able to rewrite the createHoles function as follows:

JavaScript:
Spriteset_Excavating.prototype.createHoles = function(){
  if(hammerEquipped){
    var hammerPower = $gameVariables.value(10); //in this case equals 8 for what I plan to go over next.
    this._tileList = [];
    if(TouchInput.x >= 48 && TouchInput.x <= 768 && TouchInput.y >= 96 && TouchInput.y <= 512){
      let i = Math.round(TouchInput.x / 32);
      let j = Math.round(TouchInput.y / 32);
        const tile = {
          tileID: {
            id: i + j - 1,
            tileIsThere: true
          },
            x: 32 * i,
            y: 32 * j
        }
          const tileSprite = new Sprite();
          tileSprite.bitmap = ImageManager.loadHole("RockHole");
          tileSprite.move(tile.x - 16, tile.y - 16, Graphics.width, Graphics.height);
          this._rock.addChild(tileSprite);
          this._tileList.push(tile);       
          }
        }
    };

Below is what I plan to do next with the hammer's effect: A way to create a series of tessellating hexagons that stretches from a negative and positive value of half the hammerPower variable's value. I might know just the method, but I decided to post this to the thread just in case I get it wrong.

1655597459606.png
With the answers given I was able to rewrite the createHoles function as follows:

JavaScript:
Spriteset_Excavating.prototype.createHoles = function(){
  if(hammerEquipped){
    var hammerPower = $gameVariables.value(10); //in this case equals 8 for what I plan to go over next.
    this._tileList = [];
    if(TouchInput.x >= 48 && TouchInput.x <= 768 && TouchInput.y >= 96 && TouchInput.y <= 512){
      let i = Math.round(TouchInput.x / 32);
      let j = Math.round(TouchInput.y / 32);
        const tile = {
          tileID: {
            id: i + j - 1,
            tileIsThere: true
          },
            x: 32 * i,
            y: 32 * j
        }
          const tileSprite = new Sprite();
          tileSprite.bitmap = ImageManager.loadHole("RockHole");
          tileSprite.move(tile.x - 16, tile.y - 16, Graphics.width, Graphics.height);
          this._rock.addChild(tileSprite);
          this._tileList.push(tile);      
          }
        }
    };

Below is what I plan to do next with the hammer's effect: A way to create a series of tessellating hexagons that stretches from a negative and positive value of half the hammerPower variable's value. I might know just the method, but I decided to post this to the thread just in case I get it wrong.

View attachment 229749
cool, now in your window script add this in the initialize section.

this.opacity = 0;
this.y += 15;
this should give you a bit more space to work with by moving down the window and removing the outer part of the thingy.
cool, now in your window script add this in the initialize section.

this.opacity = 0;
this.y += 15;
this should give you a bit more space to work with by moving down the window and removing the outer part of the thingy.

Thanks for the tip. Now it’s onto adding a new hole sprite depending on the location the mouse clicks and if there is a hole sprite already there. Any tips on that?
This is what I've got so far:
Take 1:
1655667809039.png


Take 2:
1655670094093.png


Now it's onto the chisel tool, which as you might have guessed works similarly to the hammer but on a smaller scale.
I just came up with a better method than adding children based on Touch Input's current position on screen. What if I made a simple JavaScript Tile Map, checked if the Touch Input's current position is hovering over a particular tile on said map, and change that tile (as well as surrounding tiles) on click?

The tools shown on the command window do the following:

Cloth removes any loose rock at the bottommost layer of rock without damaging the fossil.

Drill slowly removes rock regardless of layer, but can damage the fossil.

Hammer works differently. Since it is this tool that affects surrounding tiles from the one clicked. If you look at the example image below, if point A is clicked on a certain number of times equal to the rock's toughness or if toughness <= 2, then the tile clicked on changes to the corresponding image based on current layer it is on. The radius of surrounding tiles is based on the rock's toughness divided by 2 rounded. If point B is clicked somewhere within point A's radius, however, it will add more to the impact radius. Thus, multiplying the radius of surrounding tiles changed by the distance between point A and point B.
1655708369431.png


Chisel is at a much smaller scale to Hammer and, like the drill, can damage the fossil if not careful.

Could this approach be posible with the current JavaScript available to RPG Maker MZ?

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-faster-more-efficient-way-to-add-multiple-of-the-same-child.148592/