(SOLVED) Having a difficult time centering some text


(TaylorAnderson) #1

Hey all,

I’ve been working on a project for a while now and the thing that has kept bugging me is that my score has a really hard time staying in the center of the screen. This code:

private var textObj:Text

public function ScoreBox() 
{
	Text.font = "My-Font"
	textObj = new Text(Global.score.toString())
	textObj.scale = 2
	textObj.align = "center"
	textObj.centerOO();
	x = FP.halfWidth
	y = FP.halfHeight

	graphic = textObj;
}

override public function update():void
{
	textObj.text = Global.score.toString();
}

Produces this:

(notice the off center number in the center) This isn’t even the most egregious of off-centering, as the text gets into triple digits it moves all the way to the right.

Basically all I want to know is why this is happening / how can I fix it?

Thanks, Taylor Anderson


(azrafe7) #2

This might need some investigation (not sure it’s a bug or not)…

Temp fix that should work:

override public function update():void 
{
	super.update();
	
	var changed:Boolean = false;
	
	if (Input.check(Key.UP)) {
		changed = true;
		score++;
	}
	if (Input.check(Key.DOWN)) {
		changed = true;
		score--;
	}
	
	if (changed) {
		text.centerOO();
		text.originX -= 1;
		text.originY -= 1;
		text.text = "" + score;
	}	
}

Basically centerOO() seems to be off by 1x1 pixels (haven’t tested with a different font though).

FPTestTextCenter.swf (86.9 KB)


(TaylorAnderson) #3

that worked perfectly! thank you! i will have to remember that text.centerorigin is one px off.


(azrafe7) #4

I’ve tried to look further into it. It’s not exactly a bug (so at the moment I don’t feel like it needs a patch). I may be wrong, but it seems to simply have to do with rounding/float division (see Image.centerOrigin()) :wink:.