puzzler

The subtleties of adding children

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.