I’m finishing up a simple game called Froggy Went A Hoppin’ that’s for the Beginning Mobile Game Development course I’m creating. There are multiple levels where you need to hop a frog from one lily-pad to the next to get him across the pond.
For the first several levels I just created them by hand — after all, there are only 24 positions per level. This is what that level looks like in code:
1 2 3 4 |
3,0,0,2,4,0, 0,1,0,2,0,1, 1,2,1,1,1,0, 0,1,0,2,0,0 |
A zero is a blank space, 1 is a normal lily pad, 2 is a rock, 3 is where the frog starts, and 4 is the finish line, an orange lily pad.
When I got to thinking about the dozen or three levels that I still needed to complete I decided to create an editor so I could tap my way to level success. And instead of creating a stand-alone editor I decided to tuck it away inside the game itself so if you know the “secret knock” you can get in.
This isn’t the first time I’ve done that. In my Roly-Polies game there’s an editor. At the title screen if you take (and kill!) one of the Roly-Polies bouncing around, and then tap the copyright line at the bottom, an Editor button appears. That puts you into the levels with a drawer that slides out and allows you to place new pieces.
The first thing I did in Froggy was to add a global variable called editorActive with a default of false. Then on my title screen I added an event listener to the title graphic of the game. In the listener I just checked to see if there was a double-tap:
1 2 3 |
local function checkTaps(event) editorActive = event.numTaps == 2 end |
Now if the user does a double-tap on the title graphic that global variable gets set to true.
There are two main places where I check the value of that variable.
First, in the level selection screen if editorActive then I show all the levels, otherwise I only show the ones completed — the rest have a lock on them. That way I can edit any level whether I’ve played it or not.
When I choose a level I go into the play scene. Based on editorActive I either display the objects normally or in edit mode. The difference is in edit mode I show the blank spaces and I also put a touch listener on every object.
When a touch happens, I just cycle through the available objects. So if I click a lily pad it will change to a rock. Click again and it changes to a teal lily-pad (that I use to show where the frog should start), another click shows the finish line lily pad, and another click shows the blank space. And it starts all over.
The other thing I do when I go into play and find editorActive is “hijack” the score text in the top-right. I change the text and add a listener. When I tap that I loop through the table that holds the level objects and spit out (into the terminal) a list of numbers like you saw up above. I can then go paste those into the code.
I also can back out to the main menu (which resets editorActive to false) and then go back in and play my new level.
I decided to add one more feature so I could build levels on my iPad while away from the desk. When I tap that Print Level Nums object I pull the level numbers together, take a screen shot of the newly created level, and email those things to myself.
1 2 3 4 5 6 7 8 9 10 11 |
local baseDir = system.DocumentsDirectory display.save( group, "level.jpg", baseDir ) local options = { to = "jay@example.com", subject = "Froggy Level " .. tostring(currLevel), body = "Level. " .. tostring(currLevel) .. "\n" .. data, attachment = { baseDir=system.DocumentsDirectory, filename="level.jpg", type="image" }, } native.showPopup("mail", options) |
Now I can work on levels anywhere at all and when I come up with something good I can shoot it back to the office and deal with it when I get back.
Won’t People Find The Editor?
Before I ship Froggy I’ll probably add one more step to make sure people don’t find the editor by accident. Maybe double-tap the title graphic and then double-tap the frog’s left eye, or something like that. I don’t mind users going in and editing (especially in this, a sample app) but it might not be good if they end up there by accident. Trying to play the game and things aren’t working… 😉
I left the editor inside Roly-Polies on purpose and if people join the “RP Fan Club” (there’s a link in the game) I tell them how to find the editor.
While stand-alone level editors are kind of normal, if you’re making a game where an in-game editor makes sense, take advantage of the fact that you have most of the code in place anyway.