[Updated August 2, 2020]
This is a utility function for Solar2D called makeDriftingText() that creates text on the screen that slowly drifts up (or down) and vanishes.
You can use it when the guy in your game grabs a coin to show how many points it’s worth, you can show a message on screen when your rocket ship reaches a certain level, etc. It’s a quick little function that makes it easy to add some pizazz to your game.
Wanna Pass Parameters? Well, Do Ya, Punk?
If you’re feeling lazy this is how you can call the function:
1 |
makeDriftingText("Stage 3 Cleared") |
That will spawn the message in the center of the screen where it will drift up 40 pixels over 2 seconds while it fades from view. If you want to specify the location of the drifting text, how far and fast it drifts, etc., you can pass in a table with multiple parameters, such as this:
1 |
makeDriftingText("Stage 3 Cleared", {del=1000, t=1000, yVal=-50, x=20, y=300} ) |
Now the message will appear down 300 pixels and close to the left edge, delay for 1 second, and then drift up 50 pixels over the course of 1 second.
The day I learned how to pass optional parameters in Solar2D was a red-letter day — it’s a technique that opens up so many possibilites. The way it works is simply by checking a variable to see if it’s nill and if it is, set it to a default.
One way to do that would be:
1 2 3 4 5 6 |
local fontFace if myFont == nil then fontFace = "Helvetica" else fontFace = myFont end |
While that works, all you really need is a one-liner:
1 |
local fontFace = myFont or "Helvetica" |
If you pass in a parameter named myFont it will be used as the value for the variable fontFace, but if myFont is missing it will set fontFace to Helvetica. Whether you pass in a parameter or not, the local variable fontFace will be set correctly.
If you already knew how to do that you’re shrugging your shoulders. If this is new to you, your brain probably just exploded as you realized how cool it is.
Passing A Table of Parameters
Here are the first several lines of the makeDriftingText() function:
1 2 3 4 5 6 7 8 9 |
local function makeDriftingText(txt, opts) local opts = opts or {} local dTime = opts.t or 2000 -- drift time local del = opts.del or 0 -- delay time local yVal = opts.yVal or -40 -- how far to drift local x = opts.x or display.contentCenterX -- initial X location of text local y = opts.y or display.contentCenterY -- initial Y location of text local fontFace = opts.font or "Helvetica" -- font to use local fontSize = opts.size or 18 -- font size |
The txt parameter is the message (points, etc) to display. The opts parameter is a table holding the optional parameters. The first line of the routine creates a local variable called opts, which is the same name as the variable we pass in. If we actually passed in an opts table, we set our local variable to that value, otherwise we set our local variable to an empty table. That’s so none of the following lines complain if a table wasn’t passed in.
The rest of the routine is just as simple. We create the text object with display.newText() and then start it animating and vanishing with transition.to() where we use the onComplete callback to call a function that kills the display object and releases that memory.
Want To See It In Action?
The makeDriftingText() function is part of my Beginning Mobile Game Development video course on Udemy but I’ve made those two videos free to view. Go to the following URL and scroll down to Section 12 and look for Make Drifting Text Part 1 and Part 2: http://www.udemy.com/beginning-mobile-game-development (link opens in a new window/tab)
And Do You Want The Source Code?
If you’re a member of this site, GDN, make sure you’re logged in and you should see a link right below here where you can download the code for this cool function. It comes with a little demo program so you can see it in action.
Click here to join GDN (free!) to download the project files (or log in if you’re already a member). After logging in you will see the link below,
Originally written for Corona SDK which is now called Solar2D.
I’m signed in, but there is no link to the source.
You’re in the database, but you haven’t verified your account. Look for an email from GDN – there will be a link in that email to verify your account. Click it.
If you don’t see an email, check your spam/junk folder because maybe it got waylaid.