Jump to content

.addChild Behaviour


Yamist
 Share

Recommended Posts

Hi, I'm messing around with Phaser and I'm noticing that the .addChild function seems to associate the children with the parent object more than I thought. Specifically this code:


//Sets the alpha of both the player and the weapon to 0.5
player.addChild(weapon);
player.alpha = 0.5;

Is this intended, and if so, is there a way to avoid this? I want to make my player sprite flash when hit, but not the weapon. As it stands my weapon is a child of the player character. Any help would be greatly appreciated.

Link to comment
Share on other sites

Yup, this is intended. When you add a child to its parent it adopts a lot of the parent's properties: its display offset in the world, its alpha, its tint, etc. If you don't want that you could make a parent that holds the player sprite and the weapon, then set the alpha on the player by itself.

This is part of the display list idea. You can think of every display object in your game as sitting somewhere in a tree, with some things coming before another (siblings in the tree) and other things *containing* other things (parent-child relationships). At its root is the stage. Right beneath that (usually) is the world. Pretty much everything else is in the world, certainly anything you created using one of the "game.add.whatever" methods.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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