# Drawing Text on a scene

I am not sure if I am missing something but I was thinking about how text entities are drawn on a scene, are they drawn with an offset depending on the length of the text? I notice that when I add a new Text element at say new Text(10, 20,..... and the actual text content is 50 words long the text is drawn far over to the right and when I use the same instance with a smaller text content that text is drawn closer to the actual coordinates I need. Should I always recalculate X and Y vectors with respect to the current portion of the scene being shown on the camera?

Words:
String words = "Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here. " +
"Hello, I see that your new here.";

//method call
createSpeech( BodyA.getPosition().x, BodyA.getPosition().y, words );

//method definition
private void createSpeech(float xVal, float yVal, String ptext) {

xVal = xVal * GameConstants.PIXEL_TO_METER_RATIO_DEFAULT;
yVal = yVal * GameConstants.PIXEL_TO_METER_RATIO_DEFAULT;

speech = new TickerText( 0, 0, ResourceManager.getInstance().font,
words, new TickerText.TickerTextOptions( 10 ), ResourceManager.getInstance().vertexManager );

speech.registerEntityModifier( new AlphaModifier( 5, 0.0f, 1.0f ) );
speech.setBlendFunction( GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA );
speech.setScale( 0.5f );
speech.setHorizontalAlign( HorizontalAlign.CENTER );
speech.setAutoWrapWidth( 500f );
speech.setAutoWrap( AutoWrap.WORDS );

speech.setPosition( xVal, yVal );


Screenshots: The Red and orange lines indicate where the text is shown currently, the blue region is where I want the text to be shown. The calculations I used to set the text position and the text itself is shown above

I think the text object itself might have something to do with the offset positioning because when i use a smaller string value the position of the text changes as well

• Image? I'm having trouble envisioning precisely what you mean by "drawn far over to the right." – Sean Middleditch Sep 26 '13 at 20:56
• updated @SeanMiddleditch I hope this helps explain my situation – kabuto178 Sep 26 '13 at 21:34

Yes, you need to recalculate position of text entity. The Text entity has its own height and width, and the height/width will be updated automatically after you reassign string for the same text entity. (The height/width is like the orange rectangle showing in your picture)

When you call text_entity = new Text(10, 20, ...) and yourScene.attachChild(text_entity), it means you place the "anchor point of text entity" to the "point of your scene" ((10, 20) in this case). Depending on your andengine version, the anchor point of any entity may be on the top-left corner (GLES2 branch) or horizontal/vertical center (GLES2-AnchorCenter branch).

Assume you're using GLES2-AnchorCenter branch, and you need your char_entity and text_entity to be left-aligned, and vertical connected. The text_entity's new x, y as below: (remember the anchor point is in the center of entity)

x of text_entity = char_entity.x - char_entity.width * 0.5f + text_entity.width * 0.5f
y of text_entity = char_entity.y + char_entity.height * 0.5f + text_entity.height * 0.5f


Hope this help.

Update calculation for GLES2 branch (anchor point is located at top-left corner, y decrease up toward)

x of text_entity = char_entity.x
y of text_entity = char_entity.y - text_entity.height

• I am actually using GLES2 2 Branch now, will the calculations be the same? – kabuto178 Sep 27 '13 at 7:44
• No, I update the calculations for GLES2 branch. (left-aligned, text entity is above char entity and vertically connected.) – 正宗白布鞋 Sep 27 '13 at 8:10
• The text X value is still being printed as seen in the picture far off from the char entity, as this is how I had it in my current code – kabuto178 Sep 27 '13 at 8:17
• Do your text and char entities both have a common parent entity (ex: the same scene) ? I was assuming they have a common entity. – 正宗白布鞋 Sep 27 '13 at 8:40
• Yes, i attach the text to the main scene, and I attach the text to the main scene. I have made a small rectangle to text the X and Y coordinates and the test rectangle actually shows the X and Y in the position I desire, so now it looks as if the text entity itself is giving some sort of offset value for X and Y – kabuto178 Sep 27 '13 at 8:46