Jump to content

Phaser 1.1.4: Nested groups bug ? (PIXI object exposed when using Phaser.Group#getAt)


george
 Share

Recommended Posts

Hello phaser community,

I observed some kind of misbehaviour with group nesting. It seems that the reference for the nested group is lost and PIXI.DisplayObjectContainer is exposed instead.

 

Look at the following Testcode. There is a sprite in a group which is added itself to another group. When I try to retrieve the nested group with #getAt it returns a PIXI.DisplayObjectContainer and not the Phaser.Group object. I think this is not an expected behaviour ?

 

I digged that deep because my initial problem was (and still is) that #countDead is not working with nested group items because of this behaviour (It's not iterating over the nested group elements but over the corresponding PIXI Containers., with the lack of the alive attribute). I couldn't find a back reference from DisplayObjectContainer to the Phaser.Group object. With this I could have build a workaround but I couldn't find anything to map PIXI objects to Phaser objects.

//create a base group to start withbaseGroup = @game.add.group()//add another nested group (to create the misbehaviour)nestedGroup = @game.add.group()baseGroup.add(nestedGroup)//now create a sprite to have a non-group type to addsprite = @game.add.sprite(0, 0, 'cap');//add it to the nested groupnestedGroup.add(sprite)//Now let's do some testing://output: sprite should be found in child group: expected: (true) is trueconsole.log 'sprite should be found in child group: expected: (true) is', nestedGroup.getAt(0) == sprite//output: base group should container the nested group: expected: (true) is false console.log 'base group should container the nested group: expected: (true) is', baseGroup.getAt(0) == nestedGroup //output: group#getAt should not expose PIXI Container: expected: (Phaser.Group) is (PIXI.DisplayObjectContainer)console.log 'group#getAt should not expose PIXI Container: expected: (Phaser.Group) is', baseGroup.getAt(0) //Test with countDead which uses group#iterate//First of all: set sprite dead. This is only a flag and should be reflected in method group#countDead.sprite.alive = false//default ist already true, for the sake of consistency.nestedGroup.alive = true//Sprite is DEAD, nestedGroup is ALIVE. Test it://output: our sprite is dead, so this should be 1 is 1 console.log 'our sprite is dead, so this should be 1 is', nestedGroup.countDead()//output: our nested group is alive should be 0 is 1 console.log 'our nested group is alive should be 0 is', baseGroup.countDead()

I know that there was an update in a commit between 1.1.3 and 1.1.4 (see this commit). It seems to be related, because of the following selected code line. When nesting a group it's unerlaying PIXI object is added. But the reference to the Phaser.Group is lost at this point isn't it ? So all following calls to group#getAt(index) is return a PIX object and not the Phaser.Group object.

add: function (child) {        if (child.group !== this)        {            if (child.type && child.type === Phaser.GROUP)            {                child.group = this;                // Do we lose the reference to the Phaser.Group itself here ?                this._container.addChild(child._container); 

(Source: github)

 

My workaround for now is to maintain a own list/array of group items. It would be great to depend on Phaser's own display list.

 

Thanks for reading.

Regards George

Link to comment
Share on other sites

  • 4 weeks later...

Hi George,

 

I am also encountering some error with nested groups

gameGroup = this.game.add.group();platforms = this.game.add.group();gameGroup.add(platforms);

I am using Phaser 1.1.3 and I am receiving error:

TypeError: e is undefined

 

 

Have you encountered this error with nested groups?

 

Thank you :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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