Corona SDK Pro Tip #36
SmartPixel config.lua

You all know the config.lua file and you know how important it is. However, using the standard configuration makes your apps and games a little blurry on devices with different resolution than iPhone 4 or 5.

Here is the standard config.lua.

Standard config.lua

application = {  
    content = {
        width = 320,
        height = 480,
        scale = 'letterbox',
        fps = 60,
        imageSuffix = {
            ['@2x'] = 1.1,
            ['@4x'] = 2.1
        }
    }
}

The problem here is that content width and content height are fixed. And on iPad the game will be a little blurry. But to make it pixel perfect for iPad you must specify 384x512 as the content size. Being multiplied by 2 that would result in 768x1024 - native screen resolution of non-retina iPad.

● ● ●
CONTINUE READING

Corona SDK Pro Tip of the Day #35
Dynamic masks without display.newContainer()

Masks are extremely useful for building a rich UI or some game elements. However it's use in Corona SDK is somewhat limited. To use a mask we must create a specific PNG file with size equal to masking area.

You can end up with many mask files for all kind of sizes. Wouldn't it be cool if you didn't need all these files? And Corona Labs introduced display.newContainer() which is a group with a resizable mask glued to it. You can change the size of the container and you don't need to use many mask files.

However there is one downside using containers - coordinates x = 0, y = 0 is the center of the container and you can't change it to say the top left corner. So if you rely on the top left positioning of the elements in the container - you have to subtract half width and half height of the container from position of every object inside the container. Sometimes it's very hard to do and the code becomes very hard to follow.

● ● ●
CONTINUE READING

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