Player movement using a finite state machine – Part 1

Welcome to the first part in a new series of tutorials on how to setup a player movement state machine in Lua using CoronaSDK.

If you haven’t yet come across them, you soon will! Finite state machines are everywhere and extremely powerful. Don’t worry though, they are surprisingly easy to implement.

A finite state machine can only be in one state at a time and can be changed to other states through certain events you might have within your application. In the case of a game, you might want to have a state machine to handle a player’s movement – idle, left, right, jumping, landing, etc.

Why bother though? When you start a new project, it might not be apparent why you would even need a state machine. But a game can be an incredibly complex piece of software that can grow quickly into a monster with bugs everywhere! Without robust design patterns integrated, you can soon find yourself in a pickle!
Continue reading

Posted in CoronaSDK, Games, Lua, Mobile, Tutorials | Leave a comment

Create a parallax scrolling background for your mobile game

Parallax scrolling backgrounds have become extremely popular in all forms of media, and it’s easy to see why. They give the illusion of depth in a 2D environment, that can really enhance the user’s experience. They have been around in games for a long time, and it’s a very easy feature to implement.

Here is a very simple snippet that is easily modifiable that you should be able to use in your own projects. Please feel free to extract the code and use as you wish:
Continue reading

Posted in CoronaSDK, Games, Lua, Mobile, Tutorials | Leave a comment

Create a custom activity indicator in Corona.

Whenever an app processes some data, or waits for a response from a call to a web service, the normal approach is to use an activity indicator to feedback to the user that the application is busy. Without this, the user might think the application has crashed, and proceed to “screen bash” until it responds.

Adding an activity indicator is extremely simple in Corona. All you have to do is call “native.setActivityIndicator(state)”, where state is a boolean, true or false. True initiates the activity indicator, false will remove it.

This on it’s own works great, and for most apps, you won’t need anything more. But, perhaps you want to display a message along with an activity indicator to feedback to the user what the application is doing. Perhaps it is playing a move in a game, or perhaps it is sending a message to a user. Corona and Lua makes doing this sort of thing extremely easy.

Let’s find out how.
Continue reading

Posted in CoronaSDK, Lua, Mobile, Tutorials | Leave a comment

Why I switched to Corona SDK

In order to fully explain my reasons for switching to Corona SDK, I think it’s important to cover how I discovered Corona, and the frameworks I tried before settling.

A few years back I was hard at work developing web and mobile games in Flash, as well as playing around with various other mobile technologies. At the time, I was comfortable with Actionscript 3.0, since I wanted to do more mobile work, I naturally looked for ways in which to build mobile applications using my existing skill set. To my surprise, there was a lot of resources and options for AS3 developers wanting to move into mobile.
Continue reading

Posted in CoronaSDK, Flash, Games, Mobile | Leave a comment

Object oriented programming in Corona SDK – Part 1: Basics

I’ve been reading a lot of requests lately for ways in which Corona SDK can be programmed in an Object Oriented approach, and thought I would start a small series of tutorials on it in the hope that it might help some people out.

Firstly, this is not meant to be an introduction to object oriented programming, and I assume you’re already familiar with it’s principles. If you’re not, there are loads of resources online to help you out, google is your friend!

The great thing about Lua (the language of Corona), is that’s it’s an incredibly flexible language, designed to be extensible, making all sorts of things possible – all you need to know is how to use it properly.

So let’s start with something simple, and create a class for a Crate, in Corona using Lua.
Continue reading

Posted in CoronaSDK, Games, Mobile, Tutorials | Leave a comment

Using the Lua Underscore library in Corona SDK

A few months back, a colleague of mine shone a light on a neat lua library I’ve since been using, called “underscore“.

Underscore is a lua library that provides a set of utility functions for dealing with iterators, arrays, tables, and functions. It’s heavily inspired by it’s javascript counterpart underscore.js

Initially, I found the syntax quite difficult to get to grips with. But after no more than a day of using it, I soon fell in love! Not only does it make your code more readable, it makes it quicker and easier to write. You’ll get the hang of it quickly, and wonder how you managed without it for so long!
Continue reading

Posted in CoronaSDK, Lua, Mobile, Tutorials | Leave a comment

Top 5 tips for marketing your mobile app

Our good friends over at Native Tongue have put together this useful and short video to help you market your mobile app.

The tips are really useful, and definitely worth a watch!

Posted in Mobile | Leave a comment

How to generate a key for your android app

Generating a key for your Android app is something which when presented with the first time, can be a little daunting. But fear not, it is incredibly simple to do and takes less than a few minutes!

So why might you need to generate a key in the first place? You can build debug versions of your android app using the provided debug key provided, but if you want to distribute your app onto Google Play and beyond, you’ll need to have your own key.

This is a tutorial for Mac users, if you’re running Windows then Google is your best friend here.

Follow the steps below to get your own key.

- Open a terminal window
- Type in “keytool -genkey -v -keystore yourappname.keystore -alias appnamealias -keyalg RSA -keysize 2048 -validity 10000″

Note: in the line above, you must replace the bold parts with your app information. So if I was making angry birds, I might have something like this:
“Type in keytool -genkey -v -keystore angrybirds.keystore -alias angrybirds -keyalg RSA -keysize 2048 -validity 10000″

Hit enter when you’re ready.

The next few screens that you’re presented with are personal, and location based questions. Follow them through and fill in the relevant information. When you’re asked for the two letter country code, for me I used United Kingdom so “UK”, the United States would be “US”, Australia, “AU”.

Once that’s completed and you’ve re-entered your password (if you specified one), the keystore should’ve been created! To locate it on your mac, use Spotlight (the magnifying glass in the top right corner) and search for “.keystore”. Move it somewhere safe and you’re good to go.


Posted in Mobile, Tutorials | Leave a comment

Shootin’ Shapes Released on Android

Over the last few weeks I’ve been working on a mini game called “Shootin’ Shapes” built using Corona SDK. I’m happy to announce that I’ve just released it onto Android stores!

Don’t forget to like me on Facebook for more updates on this and my upcoming games

Check it out and enjoy!



Posted in CoronaSDK, Games, Mobile | Leave a comment

How to create a cart with Box2D and Corona SDK

Last week I got an email from someone asking how to create a cart using Box2D in Corona. While there is a lot of resources available online for things like this, they tend to be written in C++ or ActionScript. The naming conventions can be different in these tutorials as the guys at Corona have removed some of the hard work involved with getting Box2D up and running in a game.

So here is a quick look into getting a moveable cart built in Corona. The full source code for this tutorial is available here on Github.

1. Setup the physics environment
Firstly, we have to setup the physics world inside our Corona scene. If you haven’t already, setup a new project using storyboard and create a new scene. At the top we need to import the physics library. If we don’t do this, when we try to use physics later on in our scene, Corona won’t have the resources! So insert this line of code at the top of your scene:

local physics = require("physics")

Next, insert the following code into your enterScene() function:

local group = self.view

local physicsGroup = display.newGroup()

physics.setGravity(0, 0)

You’ll notice above that we are setting the gravity of the physics world to 0 in both x and y directions. We will change this later. When building complex physics bodies, I aways recommend to begin with no gravitational forces, and with “static” physics bodies. Once you are happy with the positioning of your bodies, you can then go back and change the gravity, and body properties.

Now we’ve got the physics setup, let’s create the floor. Insert the following code under the last snippet:

local floor = display.newRect(0, 0, display.contentWidth, 20)
floor:setFillColor(255, 0, 0)
floor.x, floor.y = display.contentCenterX, display.contentHeight - 10

physics.addBody(floor, "static", { friction=3 })

local floor_base_y = floor.y - 10

Here we are defining a variable called “floor_base_y”. We will use this variable later on to position our cart properly. If you build your application now, you should have an empty scene, with a floor running along the bottom of the screen.

2. Create the cart body
The next thing we need to do is create the cart body. Insert the code below into your scene:

local cartBody = display.newRect(0,0, 100, 30)
cartBody.x, cartBody.y = display.contentCenterX, floor_base_y - 15

physics.addBody(cartBody, "static", { filter={ groupIndex= -1 } })

You will notice that we are passing in a variable named “filter” into the cart body properties. Within that we are specifying an argument “groupIndex”. Using this variable we can group box2d bodies together. If we use a negative number like above, we are telling box2d that all bodies with an groupIndex of -1 will NOT collide with each other. If we were to give it a positive number, the bodies with that groupIndex would collide.

If you build your project now, you should see something like this:

3. Create the wheels
We’re now ready to build the wheels. It’s very similar to the code above. Insert the following code into your scene:

local radius = 15

local leftWheel = display.newCircle(0, 0, radius)
leftWheel:setFillColor(255, 0, 0)
leftWheel.x, leftWheel.y = cartBody.x - 25, floor_base_y - radius

physics.addBody(leftWheel, "static", { filter={ groupIndex= -1 }, friction=3, radius=radius })

local rightWheel = display.newCircle(0, 0, radius)
rightWheel:setFillColor(255, 0, 0)
rightWheel.x, rightWheel.y = cartBody.x + 25, floor_base_y - radius

physics.addBody(rightWheel, "static", { filter={ groupIndex= -1 }, friction=3, radius=radius })

Before we build the wheels, we have defined a variable that controls the radius of the wheels. We then use that to make the display object and the physics body. Like with the cart, we give the wheels a groupIndex of -1. This means the wheels and the cart will NOT collide with each other. If we were to give the wheels a groupIndex of -2, the wheels would collide with the cart body as it has a groupIndex of -1.

If you build your project now, you should see something like the following:

4. Time to connect the wheels to the cart!
We’re now ready to connect the wheels to the cart using “pivot” joints. Insert the following code into your scene:

local l_pivot = physics.newJoint( "pivot", cartBody, leftWheel, leftWheel.x, leftWheel.y )
local r_pivot = physics.newJoint( "pivot", cartBody, rightWheel, rightWheel.x, rightWheel.y )

l_pivot.isMotorEnabled = true
r_pivot.isMotorEnabled = true

l_pivot.maxMotorTorque = 1000
r_pivot.maxMotorTorque = 1000

Here we first create the “pivot” joints by specifying the objects to connect the cart body, and a wheel. We then specify the anchor position for this joint, in the above cases we use the center point of the wheel being connected.
We then allow the motor and set the maximum torque on each joint.

At this stage, we’re lined up our bodies and we’re happy with the way things are setup. We can go back through and change our body type’s on the cart body and wheels from “static” to “dynamic”, and give the world some gravity. Make sure you do this so the code now looks like this:

physics.setGravity(0, 9.8)
physics.addBody(cartBody, "dynamic", { filter={ groupIndex= -1 } })
physics.addBody(leftWheel, "dynamic", { filter={ groupIndex= -1 }, friction=3, radius=radius })
physics.addBody(rightWheel, "dynamic", { filter={ groupIndex= -1 }, friction=3, radius=radius })

If you build the project now, you shouldn’t see much difference between this and the last build, except there should now be some lines from the center of the wheel to the center of the cart. This indicates a joint is present:

5. Make the cart move!
At this stage, our cart is all joined up and ready, but we can’t see it in action yet, so let’s add some touch and enter frame events to the game so we can make the cart move! Insert the following code into your scene:

local applyMotorSpeed = false
local motorSpeed

local function onScreenTouched(event)
    if event.phase == "began" then
        if event.x < display.contentCenterX then motorSpeed = -5
        else motorSpeed = 5

        applyMotorSpeed = true
    elseif event.phase == "ended" then
        applyMotorSpeed = false

local function update(event)
    if applyMotorSpeed then
        l_pivot.motorSpeed = l_pivot.motorSpeed + motorSpeed
        r_pivot.motorSpeed = r_pivot.motorSpeed + motorSpeed

Runtime:addEventListener("touch", onScreenTouched)
Runtime:addEventListener("enterFrame", update)

You don’t need to worry too much about what is going on here, except in the update function where we access the pivot joints we defined earlier in our code and modify it’s motorSpeed. If you build your project now and touch the screen on the left and right, it should move around!

Hopefully this has been helpful. See if you can use joints to build a more complex cart, or even a car!

You can get the full source code from Github here.

Posted in Box2D, CoronaSDK, Games, Mobile, Tutorials | Leave a comment