Jump to content

Reset a child sprite position coordinate


Recommended Posts


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');


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

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


  • Recently Browsing   0 members

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