Anthony Miller
Anthony Miller

Reputation: 15930

how to keep positioning of text with a button?

Trying to create a button with text and can't seem to find an easy way to get them to position properly. I figured after building the button, I could just target the button's x/y/height/width for the textfield's paramters too. But trace(bStart.x) and trace(bStart.y) both return 0. What am I doing wrong?

What I have:

var bFormat:TextFormat = new TextFormat();
bFormat.font = "Arial";
bFormat.bold = true;
bFormat.color = 0x000000;
bFormat.size = 28;
bFormat.align = "center";

var bStart:Sprite = new Sprite();
bStart.graphics.beginFill(0X00FF00, 1);
bStart.graphics.drawRect(stage.stageWidth / 2 - 100, stage.stageHeight / 2 - 200, 100, 50);
bStart.graphics.endFill();
bStart.buttonMode = true;
bStart.mouseChildren = false;

var bStartText:TextField = new TextField();
bStartText.defaultTextFormat = bFormat;
bStartText.text = "Start";
bStartText.x = bStart.x;
bStartText.y = bStart.y;
bStartText.height = bStart.height;
bStartText.width = bStart.width;

stage.addChild(bStart);
bStart.addChild(bStartText);

Upvotes: 0

Views: 35

Answers (1)

M4tchB0X3r
M4tchB0X3r

Reputation: 1531

because you are not changing the x&y of bStart, you're just drawing from x&y starting points, the Sprite object itself is still at 0,0

try this

...
bStart.graphics.drawRect(stage.stageWidth / 2 - 100, stage.stageHeight / 2 - 200, 0, 0);
bStart.graphics.endFill();
bStart.x = 100;
bStart.y = 50;
...

And if youre adding the text inside the Sprite it will have the same coordinates as the sprite so no need to set the texts x,y the same.

Upvotes: 1

Related Questions