Introduction to Mobile Game Development
Written July, 2015, by Rachel J. Morris
Introduction to Mobile Game Development by Rachel J. Morris is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.moosader.com/learn/ccckc/introduction-to-mobile-game-development/.
When writing text in your application, you’re going to need a font. You can either load in a .ttf file, or use the Gideros Font Creator to prepare fonts to be used in the program. We are going to keep it simple and just load .ttf files!
You can find free-to-use fonts at Open Font Library, though make sure you keep in mind the license. Some font creators want credit for their work, and some do not require it. The same applies for when you’re using someone else’s music or art as well. (Always ask or check the license!)
Put the .ttf file somewhere within your project folder on the hard drive, and import it into your Gideros project.
Simply load the font into a variable like this:
font = TTFont.new( "Amburegul.ttf", 30 )
Then, we can create a TextField object that uses the TTFont. This is similar to how we would load a Texture for a Bitmap, where we could manipulate the Bitmap‘s position and appearance.
We can create a TextField and output it to the screen like this:
font = TTFont.new( "fonts/Amburegul.ttf", 30 ) label = TextField.new( font, "Welcome to" ) label:setPosition( 25, 50 ) stage:addChild( label ) label2 = TextField.new( font, "my project" ) label2:setPosition( 25, 100 ) stage:addChild( label2 )
Set text color
We can also adjust the text color with the setTextColor function:
label:setTextColor( 0xFF0000 )
On the class GitHub page, there are two projects with sample code for text:
- Simple Text project – Load a font, display text, and set text color.
Also check out the related Gideros documentation: