Jump to content

Bug in this.add.group(children) ?


peterhull90
 Share

Recommended Posts

I'm using Phaser 3.11.0 from cdn.jsdelivr.net, running on Firefox 61

I was trying to add a group using the Factory syntax:

var a = ..., b = ..., c = ...;
var gp = this.add.group([a, b, c]);

where a, b & c are GameObjects. According to the docs this ought to work but I find that gp has no children.

See https://jsfiddle.net/w3u65e4o/8/

When I look at the source  there is a test for making the children parameter optional. However I don't think this is right; typeof children is 'object' even if children is an array. By contrast the constructor for Group does the right thing (as far as I can see) - in which case having the same logic in the Game constructor is redundant.

Is this a bug in Phaser? (contribution guidelines said to ask here before filing an issue on github if unsure!)

Thanks,

Peter

Link to comment
Share on other sites

Certainly looks like a bug, that or we're interpreting the docs incorrectly. Still, it doesn't throw any kind of error, so seems like a silent bug. I had a thought maybe it was because of the sprites not being properly constructed - i.e they have no texture or position, but even with that the group is still empty. Also tried initialising the sprites in the array rather than adding them to it, no joy there either.

I found that you can use the the group.createMultiple method to create as many as you like, and they are created, but they still dont appear in the group when logging also ?

 

this.arr = [this.add.sprite(150, 150,'health_bar'),
this.add.sprite(100, 200,'stamina_bar'),
this.add.sprite(200, 100,'health_bar')];
        
this.grp = this.add.group();
//this.grp.createMultiple(this.arr);
this.grp.createMultiple(this.arr);
console.log(this.grp.children.entries.length); //size = 0
this.grp.children.entries.forEach(ele =>{
    console.log(ele); // nothing in console.log
});

as stated, this will make the sprites, but they're not in the group so Im not sure what's going on. 

 

edit - correction the sprites appear because they;'re already added to the scene in my example. 

Link to comment
Share on other sites

so I played around with the createMultiple:

 

create()
    {
        
        this.grp = this.add.group();
        this.grp.createMultiple({
            classType: Phaser.GameObjects.Sprite,
            key: 'health_bar',
            frame: 0,
            visible: true,
            active: true,
            repeat: 10,
            setXY: {
                x: Phaser.Math.RND.between(0, 500),
                y: Phaser.Math.RND.between(0, 500),
                stepX: 10,
                stepY: 10
            }
        });
        console.log(this.grp.children.entries.length);
        console.log(this.grp.children);
        this.grp.children.entries.forEach(ele =>{
            console.log(ele);
        });
    }

part of this was because i finally found the part of the docs that lists all the config object properties(yay!), but also because this didnt make any sense. Well, if you use createMultiple, it does in fact populate the group unlike my earlier thoughts on it.
So that's probably your best bet. 

edit- realised you're not looking for a fix as such, you want to determine if it's a bug or not. I'll let better minds inform on this one.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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