You are on page 1of 19

9/3/2011 Update: Added some changes to the process, updated screenshots, and disclaimers

regarding Java JDK v6 and the new Corona requirement of Android OS 2.2+ and ARMV7. I've
expanded on the hardware shortcoming and a few other annoyances and issues in a new post:
Where Corona SDK Falls Flat.
Developing mobile apps and games for both Android and iOS (iPhone/iPad/iTouch) is not the
easiest thing in the world. For iOS, the main option is to develop in Objective-C with Xcode in
OSX. However, if you want to develop for Android, there are a few more options - although most
of them still require recoding when porting to iOS. One of the biggest options for cross-device
development is the Adobe iOS Packager for Flash/Flex/AIR. All the code can be done in
Actionscript 3, and then deployed to either device (and for free!). The downside of this, is that
without serious tweaking and compromise, the framerate on games is subpar. In time, I'm sure
Adobe will smooth things out, however as it stands, it's very limiting to work with the packager.
Enter the Corona SDK:

Corona SDK is basically a Lua library that wraps both Android and iOS functions into a single API.
In this article, I will show you how to setup an environment for developing with Corona, complete
with code auto-completion, basic error checking, and an example of a box2d physics app running in
both the simulator and an Android device. All in less than 2 minutes. Haha ok, maybe not that
quick, but seriously, it takes about 15 minutes to be up and running, since there are a few things you
will need to download and install. Then, the actual box2d app will be less than 10 lines of code.

Requirements
Before we start, you will need to download these four things (don't worry, they are all free).
1. Lua for Windows
2. Java SDK v6 x86 (32bit) (note: version 6, not the newest which is version 7. Here is a direct
link)
3. Corona SDK
4. IntelliJ IDEA (Community Edition)
5. Corona API (by sylvanaar2)
6. (Optional) Here is the project file for this tutorial: HelloCorona.zip
7. If testing on an Android device, minimum specs are Android OS 2.2+, and ARMV7)

Installation

This is all pretty easy. First, install Lua, Corona SDK, and IntelliJ IDEA using recommended
settings (or custom, if you want to go rogue). The Corona API you downloaded will be a .zip file.
Just extract that somewhere (e.g. c:\sdks\sylvanaar2-idlua-sdk-corona). If you're curious, there's
only three files inside this folder: api-reference.html, corona_api.doclua, and coronoa_api.lua.
Now that everything is installed, feel free to open up the Corona simulator to test out some of the
provided examples:

All of these examples are stored in the Corona SDK Installation folder (e.g. C:\Program Files
(x86)\Ansca\Corona SDK\Sample Code). The main file for each project is main.lua. Of course,
this article is about developing with Corona, so let's get to it.

Setting up IntelliJ IDEA for Lua and Corona SDK


We need to enable Lua support first, so launch IntelliJ IDEA and click on File -> Settings:

When the Settings window comes up, find the Plugins option inside of the left column, and then
find the Lua plugin under the Available tab on the right. Right click on the entry, and select Install
Plugin.

Great, Lua is now supported in IDEA, complete with syntax highlighting and lots of other features.
Let's start on a Corona project. Create a new project.

Notice that Kahlua is listed, but we want to add our own. Click on the Configure... button. In the
top left corner of the Configure SDK window, click on the plus sign, and then select Lua SDK
from the menu.

Once you select Lua SDK from the menu, you will be asked to provide the location of your Lua
folder. (e.g. C:\Program Files (x86)\Lua\5.1)

Almost done. You can see that Lua 5.1.x is now listed on the left under Kahlua. Before we finish,
let's add the Corona API we downloaded and extracted. Click on the Add... button.

Now find the folder where you extracted the Corona API to (e.g. c:\sdks\sylvanaar2-idlua-sdkcorona).

That's it! Click OK until you get back to the New Project window, then make sure Lua 5.1.x is
selected before clicking Finish.

(Update 9/11/2011) - One more step...


To fully enable the code-completion, go back to File -> Settings:

Then find "Lua" on the list under "IDE Settings." There is a checkbox to "Add additional
completions." Make sure you check that box and hit ok.

Now that all of that is done, we won't have to do it again! Now you really can create a new app
within a few minutes since the tedious setup is done with. At this point, you should have your new
HelloCorona project waiting for you. Plus, you will have some great features like syntax checking
and auto-complete:

Starting a new Corona SDK project


Each Corona project is made up of at least two files: config.lua and main.lua (you will have to
create these yourself). Here's a screenshot of what this project looks like (2 lua files, and 3 images).
Important Note: Notice that all of the images and scripts are in the same folder. If you try putting
your assets into subfolders, the Corona Simulator will work as expected, but when you deploy to
your mobile device, the app will hang. It took me quite a while to figure that out on my own.

config.lua:
?

1
=
2 application
{
3
content =
4
{
width = 320,
5
height = 480,
6
scale = "zoomEven"
7
}
8 }
9
Pretty straightforward - we create an object (in Lua it's a table) for application, and inside that,
another object for content settings. The scale is set to "zoomEven" which basically scaled the
content while keeping the same aspect ratio. Here is a complete list of options for config.lua, since
this is just a very basic example. Easy, right? So let's look at the code for implementing box2d.
main.lua:
?
1 local background = display.newImage("background.png")
2
3 local ground = display.newImage("ground.png")
4 ground.y = display.contentHeight - ground.height / 2
5
6 local crate = display.newImage("crate.png")
crate.x = display.contentCenterX
7
Line 1 both loads and displays the background image (background.png) in a single call. It's the
first thing we add to the screen, since it's the background. Everything added now will be on top of
it (higher z-index).
Line 3 adds the ground image (ground.png) and then line 4 places it at the bottom of the screen.
This "display" object we keep referring to is analogous to the stage in flash. It also has similar
properties as a flash Sprite, like width, height, etc. Here is a complete list of the properties each
display object has in Corona SDK. Line 6 is pretty similar, except we place the crate.x at
display.contentCenterX (which is the same as stage.stageWidth / 2 in Actionscript 3).
?
9
10
11
12

local physics = require( "physics" )


physics.start()
physics.addBody(ground, "static")
physics.addBody(crate)

On line 9, we include physics, which is Corona SDK's wrapper for box2d (read more about it in the
guide and api). Then, on line 10 we start the physics engine, which automatically sets gravity to (0,
9.8) and updates at the frame rate Corona is set to (default is 30 fps).
Line 11 tells the physics to recognize the ground object as a physical body, and sets it to "static"
which means it will be stationary.

Line 12 adds the crate as another physics body, but uses the default type of "dynamic" which
means it can move.

Running the project


All of the code for this example is done! A total of 3 images and only 9 lines of code (10 if you
include config.lua, which could all be on a single line). There is one last thing to setup before we
can run our project. We need to edit the run configuration of the project, so first click on the dropdown menu next to the run button, and select "Edit Configurations."

In the Run/Debug Configurations window that pops up, set the Lua interpreter to the Corona
Simulator.exe file, otherwise you will get errors when you try to run your code through the default
Lua interpreter:

Now just click on the Run button, or go to the Run -> Run (Shift + F10) menu in IntelliJ IDEA.

A command prompt will show up, letting you know that IDEA is launching your main.lua file with
the Corona Simulator (using config.lua for display settings).

You will also see the Simulator show up, where you can select various phones in the menu.

Speaking of the Simulator window, now that we can see our code in action, it looks pretty good..
but how will it look on an actual Android device? Let's find out right now! Click on the File ->
Build menu, and fill out your Package name, Target OS, and the Save to Folder. I have mine
building directly into my Dropbox folder, since I also have Dropbox installed on my Android. This
means that as soon as I'm done building, I just open Dropbox on my phone and install the .apk file.
It really couldn't be any easier.

After you click the Build button, Corona SDK will actually connect to their build servers and
generate the apk there. Once the build process is done, the .apk gets sent back, and copied to your
Save to Folder. I imagine the same process could be done for iOS eventually, but that's not a
feature on the current Corona SDK roadmap.
So, that's it! You now have IntelliJ IDEA setup for Lua and Corona development. All you need to
do is create a new project, add your assets, config.lua and main.lua files, and hit run. Once it looks
good, just build it!

Alternate Way of Launching Simulator


Update 04/24/2012 - The process outlined above makes use of the built-in launching feature of
IntelliJ. However, there is a much better solution to launch the Corona SDK simulator from within
IntelliJ IDEA, which allows the output window to show line breaks. This method makes debugging
much easier, and it's the method I'm using, instead of hitting the "Run" button. First, copy the
following code, paste it into a new text file, and save the file as "CoronaLauncher.vbs"
Note: This updated section (04/24/2012) is using IntelliJ IDEA 11.1, and some of the dialogs have
changed slightly. If you are using the older IntelliJ IDEA, all of these settings are in the same
places, but the buttons/checkboxes may have been rearranged. Feel free to comment if you need
help setting up.
CoronaLauncher.vbs
Note: Lines 12 and 21 should point to your Corona Simulator.exe file. If you are running
Windows 64-bit, this will probably need to be changed to C:\Program Files (x86)\Ansca\...
?
1 Set WshShell = WScript.CreateObject ("WScript.Shell")

2
3
4
5
6
7
8
9 Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from
1 Win32_Process")
0 'Check if Corona is already running
11For Each objProcess in colProcessList
1 If objProcess.name = "Corona Simulator.exe" then
2 vFound = True
1 End if
Next
3 If vFound = True then
1 'If Corona is already running, then switch to it and refresh
4 WshShell.AppActivate "C:\Program Files\Ansca\Corona SDK\Corona Simulator.exe"
1 WScript.Sleep 100
"{ESC}"
5 WshShell.SendKeys
WScript.Sleep 100
1 WshShell.AppActivate "Corona Simulator"
6 WScript.Sleep 200
1 WshShell.SendKeys "^r"
7 Else
'If Corona isn't running, launch it with your main.lua
1 WshShell.Run """C:\Program Files\Ansca\Corona SDK\Corona Simulator.exe """ &
8 WScript.Arguments(0)
1 End If
9
2
0
2
1
2
2
Once you've created a CoronaLauncher.vbs file with the code above, open up IntelliJ IDEA and
under the File menu, select Settings.

In the settings window that comes up, find the "External Tools" section. It will probably be empty
- so click on the add button to create a new tool.

In the Edit Tool window that pops up, enter the following values. Pay attention to the Parameters:
field, and make sure you update the value so it points to where you saved the CoronaLauncher.vbs
file created earlier. Also, make sure that "Open console" is not checked.

After hitting the OK button to save, you should now see it listed to confirm you're on the right track.

Hit OK to save this change, and then open up the Settings window again. (Note: For me, when I hit
"Apply" instead of "OK" the next step couldn't be done. So just to be safe, hit OK.) When the
Settings window comes up, scroll down to the section for "Keymap." Under the External Tools

folder, the Simulator should now be listed. Right click on the entry, and select "Add Keyboard
Shortcut"

We're almost done, I promise. In this window select what you want the hotkey to be. Since I'm
coming from a background in Flash, I decided to use F5 to launch the simulator. (Note: You may get
a message under "Conflicts" that says F5 is already in use. Don't worry about that - just hit OK
anyways.

As mentioned above, if F5 had already been assigned to a shortcut, just overwrite it by clicking
Remove

That's it! You should now be able to hit F5, and your code will launch instantly in the Simulator! If
it's still not working, make sure you have set your main folder (which contains main.lua) as the
source. You can do that by right-clicking on the project folder and selecting "Source Root" under
the "Mark Directory As" option (it's the last option in the menu) as in the screenshot below.

Conclusion
I took my first stab at Lua when writing a custom DAME exporter for FlashPunk, and swore that
my next stab would be with Corona SDK. I never expected to have an IDE setup for such a smooth
build process - and I really didn't expect to port over an entire flash game in a weekend. Within 10
hours using Corona (for the first time), I had remade a game that originally took 30+ hours with
Actionscript 3. For that reason alone, I'm in love with this SDK - despite the fairly limited set of
features (so far). During this process I also had lots of help from the following places:

You might also like