Jump to content

Move all connected sprites


Recommended Posts

Hello everyone, I have this doubt (little introduction before):

I think is really helpful to use a sprite property to "store" another sprite. For example, when I have several enemies and I want all of them to have a health bar I can do something like this:

var enemies = game.add.group();
for(var i = 0; i < 5; i++){
     var enemy = game.add.sprite(0, 0, "enemy", 0);
     var health = game.add.sprite(enemy.x, enemy.y, "bar", 0);
     enemy.health = health;

By doing this, I can easily locate the health bar in the game later. For example if I want to change the bar's frame I only have to:

enemies[i].health.frame = 1;

I can do this several times, for example if I want to add a magic bar to the enemies, a text with the name, etc.

The problem is, when I want to move the enemy sprite. At this moment, I create a tween for the 'main' sprite, and another tween for each of the properties:


This is ok when you have just one property for each sprite, but when the numbers go up, it starts to look... problematic. Specially when each enemy have different properties. 

I was wondering if there's a way to move a sprite and 'all the sprites that belongs to them' at the same time with a single tween.

Thank you!


Link to comment
Share on other sites

Thank you a lot Rick, it worked pretty good.

I'm keeping my system for some things because I need to re-order some of the element's depth, but the 'addChild' function will help me with some minor elements.


One of the (many) good things Phaser have is you can have an answer from the guy who do the code. Thanks!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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