Jump to content

Move all connected sprites


Liranan
 Share

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;
     enemies.add(enemy);
}

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:

game.add.tween(enemies[i]).to({...});
game.add.tween(enemies[i].health).to({...});
game.add.tween(enemies[i].magic).to({...});

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!

Gonzalo.

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

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...
 Share

  • Recently Browsing   0 members

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