Jump to content

addChild: Displacing the child


Meowts
 Share

Recommended Posts

Hey,

 

Real simple case: draggable popup window with text.

var popup = this.game.add.sprite(coordinates[0], coordinates[1], this.popupSprite);popup.anchor.x = 0.5;popup.anchor.y = 0.5;var t = this.game.add.text(popup.x-110, popup.y-50, msg, this.msgStyle); popup.addChild(t);popup.inputEnabled = true;popup.input.enableDrag(false);

Problem: After calling addChild, it ends up rendering the text way off to a seemingly random coordinate, sometimes even off the screen. The text does move with the popup window however.

 

I played around with Groups to handle this, but the problem is that the text does not move with the popup window, even though they are in a group.

 

Any suggestions on how to handle this?

Link to comment
Share on other sites

You're adding the text to the popup but you also offset it with the text coordinates - which isn't a great solution also because the popup has an anchor.x/y = 0.5. 

 

var t = this.game.add.text(popup.x-110, popup.y-50, msg, this.msgStyle); popup.addChild(t);

 


Just try: 

var t = this.game.add.text(0,0,msg,this.msgStyle; popup.addChild(t);t.anchor.setTo(0.5, 0.5);
 

And see how that works.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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