Branlin Posted December 7, 2016 Share Posted December 7, 2016 I have the yellow sprite is located at the center of the screen. And that yellow sprite is parent of purple sprite. var parent = game.add.sprite(0, 0, 'yellow_rect'); parent.x = game.world.centerX; parent.y = game.world.centerY; parent.anchor.setTo(0.5, 0.5) var child = game.add.sprite(0, 0, 'purple_rect'); parent.addChild(child); and we see the following result: but I need the following result: How to reset the coordinates of the anchor child sprite so that it is not positioned relative to the parent center? It's possible to do that thing in a great way? Link to comment Share on other sites More sharing options...
s4m_ur4i Posted December 7, 2016 Share Posted December 7, 2016 I think the parent has direction properties, so you could add the chid to: parent.left parent.top (not tested) Link to comment Share on other sites More sharing options...
samme Posted December 8, 2016 Share Posted December 8, 2016 Either parent.anchor.setTo(0, 0); or child.alignIn(parent, Phaser.TOP_LEFT); Link to comment Share on other sites More sharing options...
Branlin Posted December 8, 2016 Author Share Posted December 8, 2016 1 hour ago, samme said: child.alignIn(parent, Phaser.TOP_LEFT); So this will only work if the "child" is no child of the "parent" sprite. In other words, that this code fails to function properly if we add: child.addChild(parent); But why do I need the child sprite? Without this, the "child" will not be changed scale if we scale his "parent". But for now I use this method: function alignTopLeftOfParent(sprite, x, y) { var parent = sprite.parent; sprite.x = sprite.x - parent.width * parent.anchor.x + x; sprite.y = sprite.y - parent.height * parent.anchor.y + y; } Link to comment Share on other sites More sharing options...
samme Posted December 8, 2016 Share Posted December 8, 2016 Good point. I think you can skip `anchor` etc. and do function alignTopLeftOfParent(sprite, x, y) { var parent = sprite.parent; sprite.x = parent.x - parent.left + x; sprite.y = parent.y - parent.top + y; } Link to comment Share on other sites More sharing options...
Recommended Posts