Jump to content

adding Sprites through iteration


grosjardin
 Share

Recommended Posts

I have an issue adding Sprites inside my Pixi app when the amount becomes to big. It works well with couple hundreds, but when it is 4000+, only a portion of the Sprite Textures will be drawn on the app.

Is looping through an array of objects to create Sprites and add them one by one a bad idea? Should I try to create all of them in firsthand and then add them to my container all at once by passing them as a an array of argument to addChild() ? What would be the best practice. 

 

Here is the code I am using. Array of 4000+ objects would be elements.

               this.container = new PIXI.ParticleContainer();
                this.viewport.addChild(this.container);
             
                    this.elements.forEach((f) => {
                        const sprite = new PIXI.Sprite(PIXI.Texture.WHITE);
                        tile.x = f.position.x;
                        tile.y = f.position.y;
                        tile.width = tile.height = this.tileSize;
                        tile.tint = 0xC1DBE3;
                        this.container.addChild(sprite);
                    });
                
Link to comment
Share on other sites

addChild() does not have any tax, its just adding them to array. Object creation can be slow if devTools are opened at that moment.

The best practice is to not use sprites for tiles. Consider dividing your map to separate parts and use Graphics beginTextureFill() or pixi-tilemap for each. For more details please search "pixi-tilemap" in this subforum. I explained it so many times that it hurts that no one made it into an article. Low-level / high-level algorithms and all that.

Edited by ivan.popelyshev
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...