Monday, January 26, 2015

Implementing Explosion with Corona SDK

The gif is about 600K so please wait a bit..

While working on my physics-based game City of Carbon, I needed to implement an explosion that send physic objects flying.  This is what I came up with.    The code for explosion part is below.  If you use it, you will have to change the hardcoded values so it fits your game

Download the completed project at

Explosion = {}

local sin = math.sin
local cos = math.cos
local random = math.random
local rad = math.rad

function Explosion:new(x, y)
    local expl = display.newGroup()
    local r = 80
    local angle = 0
    expl.x, expl.y = x, y
    local pdelay = 225/80 * r
    local balls = {}   
    function expl:exec()
        for a=0,360,30 do
            local ball = display.newRect(x, y, 10, 10)
            physics.addBody(ball, "dynamic", {density=1, bounce=0.5})
            ball.isBullet = true
            ball.linearDamping = 12 - ((r - 80) / 10)
            ball.gravityScale = 0
            ball.alpha = 1
            local ang = rad(a + angle)
            local f = 2500 / 80 * r
            ball:setLinearVelocity(f * sin(ang), f * cos(ang))
            table.insert(balls, ball)
            if a % 60 == 0 then
                local expl1 = display.newImage("smoke.png")
                expl1.rotation = -a + 180
                local s0 = random(1, 10) / 100
                local s1 = random(100, 150/80*r) / 100
                expl1:scale(s0, s0)
                local ra = rad(random(a-20, a+20))
                local r2 = random(r-15, r+15)
      , {time=pdelay, x=r2*sin(ra), y=r2*cos(ra), xScale=s1, yScale=s1,
                    onComplete=function(o) o.alpha = 0 end})
        timer.performWithDelay(pdelay, function(e)
            balls = nil
    return expl

return Explosion

Sunday, January 25, 2015

Step by step Google Play IAP implementation with Corona SDK

Corona SDK is awesome. It is super easy to use.  In fact, a few lines in Lua is all you need to implement IAP for your Android app.  However, implementation is just a small part of the whole process to get IAP working. You will spend quite a bit of time setting up your IAP on Google Play server.

If you are trying to implement IAP in your app for the first time, it is not very obvious what you need to do. There are a lot of steps as you see below, but they are quite easy and it should take less an hour to do all of them

1) Become at least basic subscriber
You will need to be Basic or higher subscriber to use IAP feature in Corona SDK.  The free version of the SDK will not build the code that uses IAP API.

2) Setup project
Next, in your build.settings, add the following permission in your build.settings

   android =
    {        usesPermissions =

Also, in plug in section

    plugins =
        [""] =
            publisherId = "com.coronalabs",
            supportedPlatforms = { android=true }

3) Build initial version of your app with release key
You will need to tell Google that your app is using IAP. So, build your app using the release key.  Your app doesn't need to be completed.  It just need to have right permissions and is signed correctly.

4) Upload apk to Google Play in Alpha or Beta test
Once you get apk file signed with release key, you will have to create a new entry for it in Google Play developer console, then upload and publish your apk to Alpha or Beta testing.

5) If not, setup merchant account
You will need to have a merchant account to use IAP.  If you don't have one, set it up now.

6) Add IAP items
Once you have merchant account setup, and if your uploaded app has the right permission, you will be able to add items that you plan to offer to your game's players.

There are two types of items, managed and unmanaged.  The difference is that Google keeps track for managed item for you and you can look it up in future.  For example, if you let the players to remove banner ads for $0.99, the players should be recover it when they factory reset the phones and reinstall your app.

As of IAP V.30, purchasing records of unmanaged items are recorded on Google's server.  When the users use the items, you will have to call the server to let it know the item is consumed.  The users won't be able to buy the same item until it is consumed.

By the way, make sure you write down the item ids.  You will need to use them in your code.

7) Add test users
 To be able to test IAP in your app while it is in beta, you will need to add test users.  Go to the APK section of your app entry and click on Manage list of tester.

In the popup dialog, you can add list of test users.  The easiest way is to create Google Plus community, invite your test user to join that community, then add URL of that community so all members become test users.

8) Implement IAP logic
Now setup of Google Play side is completed.  You have to finish the IAP part of your application. Go to You can almost completely copy-and-paste the code there.

9) Upload your app with completed IAP code to Play Store and publish it in Alpha or Beta.
Even if it's iin Beta or Alpah test, you still need to fill in the descriptions, upload icons and screenshots.  When you have done all that, you can publish the app.  Do not promote it to production yet, just keep it in Beta or Alpha.

10) Announce the test URL to your tester
In APK section of your app, click on Manage list of testers again.  This time you will see the download link for your app.  If you don't see it, that means you have not published your app.

You can send this link to your testers. Only your testers can download the app using this link. 

11) Testing the IAP without getting charged
The testers can buy IAP items without being charged.  Test it until you are sure that it is working correctly.

12) Promote to production
When ready promote to production so anyone can buy your IAP items.