Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

phaser collections in a group


pranadevil
 Share

Recommended Posts

Once you have a group "var myGroup = this.game.add.group(this.game.world, 'myGroup');"

 

You can add sprites to it: "myGroup.addChild(mySprite1);"

 

Check out the examples: http://phaser.io/examples/v2/groups/add-a-sprite-to-group

 

 When you do addChild or add, Phaser (or PIXI? I'm not sure) remove the child from its parent and add it to the group you are calling from.

 

 

hi there

 

i want to someone kindly explain me how can i have a group of different elements(sprites) in phaser.

ive been working with groups like : group1 =[ x1,x2,x3,x4,...xn] (all are X sprites)

i want to have a group like  group2 = [x1,z1,z2,y1,z3....] and so on.

 

how i can populate it? 

 

a good example will be awesome

 

Obviously, the groups aren't supposed to do that, since they serve the purpose of organizing Display Object. Do you have a use case for your need so we can give you a direction?

Link to comment
Share on other sites

 When you do addChild or add, Phaser (or PIXI? I'm not sure) remove the child from its parent and add it to the group you are calling from.

 

 

 

Obviously, the groups aren't supposed to do that, since they serve the purpose of organizing Display Object. Do you have a use case for your need so we can give you a direction?

 

 

 

imagine that i have 5 different sprites (enemies) and i want to have a group of 15 elements. i want i can populate the group and then randonmly add one the elements to the world kill it and then the process continues

 

hope you can guide me

Link to comment
Share on other sites

i want i can populate the group and then randonmly add one the elements to the world kill it and then the process continues

 

That's probably the most obscure sentence I have ever read in my entire life.

 

So, you want to:

 

1) Add elements to the group

2) Extract a random element from the group and put it in the world

3) Kill the element, and start all over again from 1)

 

...

yes ?

Link to comment
Share on other sites

but in the example all the items are 'baddie' sprite kind

 

 If you want to add different kinds of sprites into one group, then ... just do it. There should be no problem as far as your sprite classes inherit from Phaser.Sprite.

 

 In fact, you can add into group any other object that inherits from PIXI.DisplayObject in its inheritance chain. This means you can also add objects that are PIXI.DisplayObjectContainer into your group ... and group itself is PIXI.DisplayObjectContainer. So, you can add groups into group, you can add any kind of sprite that inherits from PIXI.DisplayObject, you can add Buttons, BitmapTexts, ... and many other.

 

i need to add different kind elements to the group and then iterate it to display each in the world

 

 

 You do not need to iterate through group by yourself. if your group is added to world (which is ... group!), all your objects in hierarchy are iterated and rendered by engine.

 

 

 What you are doing in Phaser is, that you build tree of display objects where groups are non-leaf nodes and sprites are leaves. If you do some transform on any element in the tree (like moving, rotating, ...) all child elements in tree are affected.

Link to comment
Share on other sites

 If you want to add different kinds of sprites into one group, then ... just do it. There should be no problem as far as your sprite classes inherit from Phaser.Sprite.

 

 In fact, you can add into group any other object that inherits from PIXI.DisplayObject in its inheritance chain. This means you can also add objects that are PIXI.DisplayObjectContainer into your group ... and group itself is PIXI.DisplayObjectContainer. So, you can add groups into group, you can add any kind of sprite that inherits from PIXI.DisplayObject, you can add Buttons, BitmapTexts, ... and many other.

 

 

 You do not need to iterate through group by yourself. if your group is added to world (which is ... group!), all your objects in hierarchy are iterated and rendered by engine.

 

 

 What you are doing in Phaser is, that you build tree of display objects where groups are non-leaf nodes and sprites are leaves. If you do some transform on any element in the tree (like moving, rotating, ...) all child elements in tree are affected.

how i can do it?

 

got this:

 this.s.createMultiple(11,'player');  ///// all the group will be filled with elements of 'player' class.
        game.time.events.loop(2200, this.addS, this);
Link to comment
Share on other sites

how i can do it?

 

got this:

 this.s.createMultiple(11,'player');  ///// all the group will be filled with elements of 'player' class.
        game.time.events.loop(2200, this.addS, this);

 

 
Either create your kind of sprite with new keyword and then add it into group with: group.add(myNewSprite).
Or use createMultiple, as in your example, but set first type of created objects with: group.classType = mySpritesClass;
 
It is also incorrect in your text, that: "all the group will be filled with elements of 'player' class". What it does is, it creates Phase.Sprite objects not player objects. The "player" parameter is just texture key - it says what visual appearance will be, but class of object is Phaser.Sprite.
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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