Gamua / Starling-Framework

The Cross Platform Game Engine
http://www.starling-framework.org
Other
2.82k stars 821 forks source link

Idea: TextureAtlas removeAllRegions #1036

Closed phanxgames closed 5 years ago

phanxgames commented 5 years ago

I have a need to reuse a TextureAtlas between levels and I wanted to remove all regions efficiently. I was using getNames() followed by an iteration calling removeRegion(), but getNames() uses a sort and didn't seem efficient.

So I was wondering if a function such as the following could be added to the TextureAtlas class:

        /** Removes all regions within the atlas. */
        public function removeAllRegions():void {
            var name:String;
            for (name in _subTextures) {
                removeRegion(name);
            }
        }

Thank you for your input and feedback on this idea.

PrimaryFeather commented 5 years ago

Thanks a lot for the suggestion! What would you say to this variation:

/** Removes all regions with names that start with the given prefix.
 *  If no arguments are given, all regions will be removed. */
public function removeRegions(prefix:String=""):void
{
    for (var name:String in _subTextures)
    {
        if (prefix == "" || name.indexOf(prefix) == 0)
            removeRegion(name);
    }
}

Should be (almost) as fast, but can be used for selective removal, too. Somewhat similar to DisplayObjectContainer.removeChildren().

If you like that, I can add that right away.

phanxgames commented 5 years ago

Love it. Thanks, Daniel.

PrimaryFeather commented 5 years ago

Done! :smile:

Again, thanks for bringing it up!