Corona SDK Pro Tip of the Day #34
Transition.to() for simple numbers

transition.to() and other similar functions are designed to work only on display objects. But what if you need to smoothly transition a simple number instead?

A recent tutorial from Corona Labs pushed me to share this method. The idea is to use a simple table instead of a display object and with help of a metatable track a variable being transitioned inside this table.

I'll take the code from that tutorial as a base and modify it to use a metatable and transition.to().

● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #33
Timer precision

For timer.performWithDelay() function, the delay time is not exact, it's affected by your app's FPS.

If you pass a delay value lower than FPS tick time, the timer listener will be invoked on the next frame.

For 60 FPS, tick time is 16.6ms, for 30 FPS - 33.3ms. If you rely on times lower than these values - it won't work.

I usually use 1 when I need something to be invoked on the next frame. For example in a collision listener.

timer.performWithDelay(1, function()  
    object:removeSelf()
end)  
● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #29
App specific variables

All Corona applications have names, App Store id, Google Play id and some other app specific variables.

I define them in my main.lua and use all over my code when needed.

My typical main.lua has this code somewhere near the top:

local app = require('lib.app')

app.name = '2048 Hex'  
app.font = 'GlasnostDemibold'  
app.fontbold = native.systemFontBold

app.iOSID = '841721448'  
app.androidID = 'com.spiralcodestudio.hexled'  
● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #28
EnterFrame manager

Often you need to use enterFrame listeners in your game. Sometimes you need many of them (for example one for every game object in your game) and keeping track of them becomes really difficult. You have to remove them manually or you will get runtime errors.

The code inside enterFrame is invoked each frame.

To gain more control over these listeners I've created a set of functions which can be called as "enterFrame manager."

● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #26
Short addEventListener()

You don't have to write second argument for object:addEventListener(eventName, object) if the listener is the same object.

Despite official documentation listing this argument as required, in fact it's not.

I love to write less code, and it is Corona's moto "write less code." So this feature saves quite a few characters.

-- Works with any display object
local circle = display.newCircle(100, 100, 10)  
function circle:touch()  
    -- Do stuff
end  
circle:addEventListener('touch')

-- And any events
scene:addEventListener('createScene')  
scene:addEventListener('enterScene')  
scene:addEventListener('didExitScene')  
CONTINUE READING

Corona SDK Pro Tip of the Day #25
Finalize event

Finalize event is extremely useful for developing. If you are familiar with C++ or other classic OOP languages, you can think about finalize event as a destructor - something that happens when a Corona Display Object is destroyed.

I use it all the time. Mostly for cancelling transitions or timers that a particular object uses.

local circle = display.newCircle(group, 100, 100, 10)

function circle:animate()  
    self.t = transition.to(self, {time = 1000, alpha = 0})
end

function circle:finalize()  
    -- This is called on circle:removeSelf() or display.remove(circle)
    if self.t then
        transition.cancel(self.t)
    end
end

circle:addEventListener('finalize')  
● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #24
Changing FPS dynamically

Did you know that you can invoke almost any arbitrary code inside config.lua? We can use it to read a txt file and set fps according to it's content. That way users can switch fps from in game settings.

Here is such config.lua

local fps = 60  
local path = system.pathForFile('fps.txt', system.DocumentsDirectory)  
local file = io.open(path, 'r')  
if file then  
    local content = file:read('*a')
    if content == '30' then
        fps = 30
    elseif content == '60' then
        fps = 60
    end
    io.close(file)
end

application = {  
    content = {
        width = 320,
        height = 480,
        scale = 'letterbox',
        fps = fps,
        imageSuffix = {
            ['@2x'] = 1.2
        }
    }
}
● ● ●
CONTINUE READING