Jump to content

Sprite vs Group


threedollarbill
 Share

Recommended Posts

Hi, I am fairly new to Phaser (formerly a Flash dev) and I am currently trying to decide whether I should use a Sprite or a Group.

Since they can both be used as a container, I am trying to figure out which one of the two I should use.

For my purpose, I simply want to have 3 separate main "screens" / "containers" (stage select screen, game screen, and win / lose screen). I plan on setting their visibility on / off, and possibly tweening them in and out of view too for a fancy transition-effect when switching between the screens.

My question is what would be better to use for this. Sprites or Groups? What would be the benefits or hindrances of each?

Link to comment
Share on other sites

You probably want group for that... although you should also check out States. They're not a display object container, but more of a logical "what's going on in my game now" kind of construct.

As far as group vs. sprite: groups have iteration and filtering methods like getFirstAlive and callAllExists, helpful in manipulating a large set of things.

Link to comment
Share on other sites

It is possible, sort of. The primary texture of the Sprite is pretty much always going to be on top because it is the root of the display tree -- it's "above" all of its children. If you don't add a primary texture then you can position the children all you want with "bringToTop" and "addChildAt", etc.

Link to comment
Share on other sites

11 minutes ago, drhayes said:

It is possible, sort of. The primary texture of the Sprite is pretty much always going to be on top because it is the root of the display tree -- it's "above" all of its children. If you don't add a primary texture then you can position the children all you want with "bringToTop" and "addChildAt", etc.

Okay thanks for the clarification.

Link to comment
Share on other sites

  • 4 weeks later...

It seems the difference is deeper. For example the width and height properties of a sprite will always report the size of its texture, ignoring completely the size and position of its children. I recently discovered I was trying to use them as containers (Old habits never die, I also have to unlearn actionscript)  on a big project and have to change all that in order to enable scalable relative positioning in my game.

So I would use groups for anything that is a container in your game.

Link to comment
Share on other sites

I now always use groups because I nearly always ended up having to convert them, mostly because I wanted to toggle the visibility of the base sprite without it affecting the children (which I thought I wouldn't want to do at the start).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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