grinmonk Posted May 23, 2016 Share Posted May 23, 2016 Hi! With this code I try to put a text object in the center of a sprite, which is in its turn is centered relatively to the world. var sprite = game.add.sprite(0, 0, 'phaser'); sprite.anchor.set(0.5); sprite.x = game.world.width * 0.5; sprite.y = game.world.height * 0.5; var text = game.make.text(sprite.width * 0.5, sprite.height * 0.5, "Yo", { font: "10px Arial", fill: "#ffffff", align: "center" }); text.anchor.set(0.5); sprite.addChild(text); But what I get is this: instead of this: What am I doing wrong and how to achive my goal? Thanks! Link to comment Share on other sites More sharing options...
grinmonk Posted May 23, 2016 Author Share Posted May 23, 2016 Solved by setting text position to (0, 0): var text = game.make.text(0, 0, "Yo", { font: "10px Arial", fill: "#ffffff", align: "center" }); Am I correct, that children are positioned relatively to parent's anchor? Link to comment Share on other sites More sharing options...
blackgames Posted May 23, 2016 Share Posted May 23, 2016 var sprite = game.add.sprite(0, 0, 'sprite'); sprite.anchor.set(0.5); sprite.x = game.world.width * 0.5; sprite.y = game.world.height * 0.5; var text = game.make.text(0, 0, "Yo", { font: "$10px Arial", fill: "#ffffff", align: "center" }); text.position.x -= text.width * 0.5; text.position.y -= text.height * 0.5; sprite.addChild(text); You need to set the text position to 0, 0 after text is created i add: text.position.x -= text.width * 0.5; text.position.y -= text.height * 0.5; for the position based on the size of the text grinmonk 1 Link to comment Share on other sites More sharing options...
grinmonk Posted May 23, 2016 Author Share Posted May 23, 2016 @blackgames, thanks for a solution! I think it suits right. But what if sprite's anchor is changed due to some adaptive positioning (for example)? Then it is going to ruin internal positioning. Link to comment Share on other sites More sharing options...
Recommended Posts