Drawing Text & HUDs
by Jon (Updated on 2016-01-19)
- Creating Fonts
- Drawing Text
- Example: Drawing the Score
- Dialog System
Text is an essential element of most games. Text conveys information to the player in real-time. This article will teach you how to draw text to the screen and how to style it.
Fonts are a term used to describe the styling of text. Stencyl uses bitmap fonts to display text. Bitmap fonts are a special kind of font in which the font is pre-rendered to pixels.
- They look consistent regardless of device or hardware.
- They draw quickly.
- Because they're pixel based, they can take on many more shapes and forms than traditional vector-based fonts.
- You can’t grow them at runtime without ruining quality. (You have to create a new font for them)
- They sometimes take up more disk space.
To create a font, use our Font Editor. You can create a new font by heading to File > Create New > Font.
This will bring up the Font Editor. Our Font Editor lets you style fonts based on both TrueType (TTF) font faces as well as images / sprite sheets.
If you'd like to learn more about creating fonts, read our Font Editor article.
Once your game has a font, the easiest way to draw text is to draw it directly via the “when drawing” event. All text drawing blocks are found under Drawing > Drawing and Drawing > Styles.
Step 1: Set a Font
Never attempt to draw text without first telling the engine what font to use. If you fail to do this, the game may crash.
Step 2: Draw the text
Now, draw the text. Pass in the text and the (x,y) coordinates where you want it to draw.
If you need to position the text more precisely or make calculations based on the text's size, we provide a few blocks to help you out under Drawing > Styles.
HUDs (Heads Up Displays)
A HUD (heads up display) is a transparent, graphical dashboard that displays on top of everything else.
HUDs usually display statistics. Think of the dashboard on your car, the one that displays how fast your car is going, how much gas you have left, your engine's temperature, etc.
HUDs do not follow the camera
One aspect of HUDs that’s unique is that they don’t follow the camera. They always draw at the same place on screen, regardless of where you are. Think of them as being this piece of glass that's on top of the game but not part of the game itself.
How to make an Actor "disobey" the camera
Have you seen this block? (under Actor > Drawing)
The ability to anchor an actor to the screen was made specifically for creating HUDs. As the name suggests, anchoring an actor makes the actor ignore the camera, so it always is drawn at the same place on screen.
HUDs aren’t a part of Stencyl or a specific feature. They’re just a common aspect of most games that they deserved special mention and to establish how to make them using the anchor block.
Example: Drawing a Timer
In this example, we’ll talk about how to do something common: drawing a timer.
Controls: Left/Right to move, Up to jump
We want to draw the Timer shown in the demo.
- The timer counts up once per second.
- No matter where you are in the level, the timer stays at the same spot on screen.
Walkthrough (Part 1) - Adding a Timer
Download this project. (Use File > Import... to import it)
Open the demo project. This project is mostly built up (run it!). All you need to do is create the Timer feature.
Open up the Timer actor and flip to its Events tab.
Add a Number attribute called Time - and make it hidden.
Add an Every N Seconds event. Make it increment the Time attribute by 1.
Add a When Drawing event. Make it draw the time, like this.
- That’s it (or is it?). Run the game, and you should now notice it drawing the timer, just like in the demo above.
Walkthrough (Part 2) - Fix the Bug
Wait, something's wrong. If you walk to the right, as the screen scrolls, the timer gets left behind. That's not right. Given what you've learned about making a HUD (heads up display), what do you need to do?
Fix up the actor's behavior, so it "sticks" to the screen.
Stuck? Here's a hint.
- Tweak the timer to increment twice a second.
- Show the timer on the right side of the screen. Don't hardcode the value - use blocks to calculate this, so it works no matter what the screen size is.
- Show the timer in the center. Properly account for cases where the text may be shorter or longer.
One of Stencyl's developers has independently created a fully-fledged dialog system. While it's technically a separate effort, it's officially endorsed by us.
Visit their site to learn more.
- Stencyl uses bitmap fonts to draw text to ensure good performance and consistent results.
- HUDs are graphical displays of information that display on top of everything else and do not obey the camera.
Challenge: Dialog Box
While Stencyl is home to a powerful dialog system, for learning purposes, you may want to implement simple dialog system on your own to check your mastery of text drawing.
Create a simple system for displaying dialog, in which you pass in a list of text (1 sentence per entry) to display.
The dialog system should display 1 sentence at a time. The player has to hit a key to proceed.
Print Article Edit Article How to Edit an Article
Bonus: Bonus points for auto-wrapping text and displaying a little blinking arrow to indicate to the player that the sentence is “finished” and the system is waiting for the player’s go to continue.
Tip: For users wanting to use the 'set opacity' block, it is supposed to be used before(put above) a text, shape or line's drawing code and after any style blocks. You can use the fill color and the stroke's color and thickness(which is activated by setting a stroke's thickness as it activates the stroke) at the same time, but for high stroke thickness settings, almost half of it will exceed the shape or line's outer boundaries.
hi, i'm new to stencyl. i know that an actor is not allowed to have no animation. so i have this exclamation mark in the scene. it's already there, after opening the project file i downloaded. how can i get rid of it. one solution might be to put in an image with alpha 0, but then i don't see the object in the scene editor. any suggestions?
I tried putting in text will some basic, large fonts for my title page. I also made the events: Set font to blah blah blah, and draw text: blah blah blah, but when I pressed "Test Game," The new window cut off my text, and when I expand the window size, the text is still cut off. Any ideas? Questions? Clarifications?
is there an alphabet tileset? that will be better
Hi. The link to the demo timer file doesn't work. cheers
To answer hosoji 's question, that's exactly what the HUD (Heads Up Display) section talks about.
This question may be rudimentary but is there a way to Draw text that follows the camera?
The block "anchor" seems to doesn't work anymore. :\
when i try to set score to score 1 it adds 2 and when i try to set score to score-1 it subtracts 3
how do i fix this???????
nvm sry forgot to add to scene