Frogger in Lua

Remember the Frogger series of tutorials where the same code was translated to different Frameworks? Well, there is a new version to be added to the list, this time courtesy of Luca Ferrara, and it’s a Lua version.

This is especially cool for Flash developers because Luca used a framework he’s developing (Shilke2d) that aims to port Starling to Moai, a Lua game development framework that can run on both iOS and Android.

If you never worked with Lua before, you probably heard that the syntax is similar to ActionScript/JavaScript and this port should help you test that theory for yourself, and this is probably the best way you’ll ever find to learn it.

So here is what you’ll need.

– Download the Moai SDK.

– Download the free ZeroBrane Studio Lua IDE.

– Download Shilke2d.

– And finally Download Luca’s port of Frogger.

Next comes the setup. These steps are for Mac users, but should be easily converted to Windows users.

– Inside the Shilke2D package you will find a folder called Shilke2D which is the library files. Copy and paste this folder inside the Frogger project you just downloaded.

– Next go to Applications or wherever you installed ZeroBranedStudio and click to show package contents. Navigate to ZeroBraneStudio/cfg/user-sample.lua and open this file in a Text Editor.

– Edit the path to moai excutable. Mine reads:

path.moai = ‘/Users/rengelbert/frameworks/moai-sdk-1.4p0/bin/osx/moai’

– Edit the language used. Mine reads:

language = “en”

– Here, you can also set the path to other common frameworks like Love and Corona.

– Rename the file to user.lua.

– Open ZeroBraneStudio and open the Frogger project and its main.lua file.

– Then go to the menu Project|Lua Interpreter and select Moai.

– You should now be able to run the game, on Project|Run.

Here is a description of Shilke2D in Luca’s own words:

Shilke2D takes birth as unofficial port for MOAI / lua of the as3 Starling framework. It exposes interfaces and logics very similar to Starling, including the juggler as animator of objects that implements IAnimatable interface.

Shilke2D allows to easily move to lua and MOAI if coming from Flash / actionscript 3

development, in particular from people already used to work with Starling.

Moreover make it easy to move between the two environments when needed. Writing the frogger sample in lua was mainly just a matter of translation from as3 code.

Shilke2D differs from Starling/Flash for simpler event handling:

– events do not bubble

– events related to displayList changes are not handled

It offers a slightly more advanced tween support compared to Starling, giving default support for:

– bezier curves

– delay tween

– sequence of tweens

– parallel execution of more tweens (synchronized)

The code of frogger written for Shilke2D is mainly a translation of the original Starling code. Main differnces are in initialization phase.

Shilke2D defines specific callbacks for game execution:

setup() called once at the beginning of the execution, as soon as shilke2D:start() is called. It’s the place where to put initialization of everything you need, like scene setup.

update(elapsedTime): called every frame, replace the ENTER_FRAME event of Flash/Starling.

onDirectDraw(): Called after the stage draw call, it’s used to make vectorial or debug draw.

onKeyboardEvent(key,down): only on desktop platforms, it’s called whenever a key is pressed or released.

touched(touch): called whenever an unhandled touch event occurs. Note that by default every touch event that hit a displayObj is handled directly by Shilke2D.

Frogger doesn’t need to implement a specific touched() function beacause all the touch events are handled by registered touch event handlers.

Have fun!