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.

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

Corona SDK Pro Tip of the Day #22
Remote console

Send print output and runtime errors from a device via network to your computer for debugging.

When you are using the Simulator it's easy to see all the error messages and print output in it's console. However on a device we have to use either Xcode Organizer for iOS or adb logcat for Android.

Fear no more! Here comes remote console script that allows you to receive all the info you need directly from your device with no hassle.

● ● ●
CONTINUE READING