Liranan Posted June 21, 2016 Share Posted June 21, 2016 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 More sharing options...
rich Posted June 21, 2016 Share Posted June 21, 2016 Put them in a Group, and move the Group. Or use Sprite.addChild to add as a child of the Sprite (will always appear OVER the Sprite, as you cannot re-order a nested display list below its parent) Link to comment Share on other sites More sharing options...
Liranan Posted June 21, 2016 Author Share Posted June 21, 2016 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 More sharing options...
Recommended Posts