Phaser: Phaser.GameObjects.Group.destroy() seems to be failing

Created on 17 Feb 2018  Â·  8Comments  Â·  Source: photonstorm/phaser

Possible bug in the API (Phaser 3.0.0)

this.balloonsArray = [];
...

generateBalloon() : void{
   let balloonGroup = this.add.group();
   balloonGroup.create(700, 100, 'misc', 'balloon';
   this.balloonsArray.push(balloonGroup);
}

update() : void{
    for(let x = 0; this.balloonsArray[x]; x++){
        let balloonGroup = this.balloonsArray[x];
        Phaser.Actions.IncX(balloonGroup.getChildren(), 1);
        if(balloonGroup.getChildren()[0].x > 750){
            balloonGroup.destroy();
            this.balloonsArray.splice(x, 1);
        }
    }
}

I would expect ballonGroup to be destroyed by balloonGroup.destroy(); but it remains on screen.

So I wonder if the Phaser.GameObjects.Group.destroy() method is failing.

Thank you!

💖 Feature Request

Most helpful comment

Thank you for submitting this feature request. We have implemented this and the feature has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

All 8 comments

From what I've seen on the source code, Group.destroy() only cleans up the group, clearing its Set of children gameobjects without calling .destroy() on them.

Maybe there can be added a destroyChildren=false parameter that if true also calls something like this on the children (or a new Actions.Destroy)?

Phaser.Actions.Call(this.children.entries, function(child) {child.destroy()});

AFAIK, this is by design. Phaser 3 avoids the parenting mess, and groups are merely ways to organize sprites together, so that you can run Actions on them, etc. Sprites are always children of the scene, so deleting a group should not delete the children.
I do agree with @agilul though, it would be nice if you could specify a boolean to force the removal of the group's children as well.

For now, one easy way is just to baloonsGroup.getChildren().map(child => child.destroy()), or similar.

I have also noticed a bug in your code: The for loop you use walks through the indices of balloonsArray, but as soon as you splice it, the indices change. If you are using ES6, a more elegant solution may be to use Array.filter().

As @Ziao said this is indeed by design, as unlike v2 Groups in v3 are not exclusive parents. A Sprite can exist in multiple Groups at the same time.

We could look at adding a destroyChildren argument, but in order for that to work, we'll have to change it so that Game Objects always emit a destroyed event, so that other Groups can listen for it and act accordingly. Which is probably a good idea anyway, as if you currently destroy a child of a Group, the Group doesn't know this has happened and will still include it in getChildren calls.

Thank you all very much. Greatly appreciated. I'll play some more with this.

So with regards parent > child relationships, would we just set the origin of all objects to the same co-ordinate and apply transforms to each object rather than have a parent child relationship and just transform the parent?

That's one way to do it. I did make a little something for people who can't
let go of the old parenting system. Sometimes it just makes more sense to
have groups behave the way they did in Phaser 2. Feel free to have a look.

https://github.com/Ziao/phaser3-interim-containers

On 20 Feb 2018 08:28, "Dougi" notifications@github.com wrote:

Thank you all very much. Greatly appreciated. I'll play some more with
this.

So with regards parent > child relationships, would we just set the origin
of all objects to the same co-ordinate and apply transforms to each object
rather than have a parent child relationship and just transform the parent?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/photonstorm/phaser/issues/3246#issuecomment-366888906,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABiZL-1PyJB16X0yNUZjdw52QduVMQSmks5tWnQtgaJpZM4SJOhS
.

Thanks very much :)

Thank you for submitting this feature request. We have implemented this and the feature has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

baloonsGroup.getChildren().map(child => child.destroy())

This does not remove all children. removes only few and remaining are still live.

Please help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cncolder picture cncolder  Â·  4Comments

samme picture samme  Â·  3Comments

lilijreey picture lilijreey  Â·  4Comments

rootasjey picture rootasjey  Â·  3Comments

Colbydude picture Colbydude  Â·  4Comments