Jump to content

The subtleties of adding children


puzzler
 Share

Recommended Posts

I'm starting to use Phaser, and am mystified by the subtle complexities of how the API differs for sprites vs groups.

Let's look at the process of creating a display element, and then adding it as a child of another display element at a given x,y.

1. Creating a sprite and adding it as a child of another sprite at a given x,y

parent.addChild(game.make.sprite(x,y,"artkey"))

2. Creating a sprite and adding it as a child of a group at a given x,y

parent.add(game.make.sprite(x,y,"artkey"))

Why do we need to use add here, instead of addChild?

3. Creating a group and adding it as a child of a sprite at a given x,y

g = parent.addChild(game.make.group());

g.x = x;

g.y =y;

Why is there no way to make a group with a given x, y in one step like sprites?  Why the need to manually set position in two extra steps?

4. Creating a group and adding it as a child of a group at a given x,y

g = parent.add(game.make.group());

g.x = x;

g.y= y;

For this, we use add and then we also need the two extra steps.

 

Did I get the taxonomy correct here?

Why is every combination different?

Thanks.

Link to comment
Share on other sites

Every display object (including Group) has an addChild method.

Only Groups have an add method. It calls Group#addChild. You should always use Group#add.

2 hours ago, puzzler said:

Why is there no way to make a group with a given x, y in one step like sprites?

Because it's not a common task. You can write your own helper or overwrite Phaser's add/make.group methods and add those arguments.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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