Category Archives: Lua

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

How to Play Audio using Corona SDK ?

Hi,

For playing audio – background music or sound effects – using Corona SDK, do the following,

1. Load the audio stream (background music or sound effects)

 backgroundMusic = audio.loadStream("bgm.mp3") 

2. Play the audio

backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 }  )  

It will play the background music on channel 1, loop infinitely, and fadein over 5 seconds. Loop 1 will set it to play once!
:)

How to get the elapsed time after app launch in Corona ?

In order to get the elapsed time after the application launch in Corona, use the following lines of code,

function elapsedTimeCalc( event )
  print ("Time elapsed since app launch---"..event.time/1000 )
end

Runtime:addEventListener("enterFrame", elapsedTimeCalc)

Thus we will get the elapsed time after the app launch in milliseconds.
:)

Destroying Box2D Physics Bodies in Corona SDK

Hi,

For destroying physics bodies in Corona SDK, use any of the following methods.

urBody:removeSelf()

OR

urBody.parent:remove( urBody )

Note :
While Box2D objects will be safely retained until the end of the current world step, their Lua references will be deleted immediately. Therefore, be careful not to accidentally delete the same Lua object more than once. This situation could occur when deleting objects involved in collisions, which can potentially have many event phases before the collision is fully resolved. The solution is simple:

local function onCollision( self, event )
        if ( event.phase == "began" ) then

                -- Check if body still exists before removing!
                if ( urBody ) then
                        urBody:removeSelf()
                        urBody = nil
                end

        end
end

:)

How to set a tilt based gravity using Corona accelerometer ?

Hi,

For setting a tilt based gravity using Corona Accelerometer, use the following sample of code.


local function urTiltFunc( event )
        physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
end

Runtime:addEventListener( "accelerometer", urTiltFunc )

:)

How to get touch location in Corona ?

Hi,

For getting touch location in Corona, use the following code,


--Your touch event listener
 local function myTouchListener( event )
        if event.phase == "began" then
        --Touch coordinates can be accessed by event.x/event.y properties of touch event
            print( "Touched!" )
            print ("Touch X = #"..event.x)
            print ("Touch Y = #"..event.y)
        end
    end

--Adding an event listener for touch
Runtime:addEventListener( "touch", myTouchListener )

How to get touch 'began', 'moved' & 'ended' in Corona ?

Hi,

For getting touch ‘began’, ‘moved’ & ‘ended’ in Corona, use the following code.

--Your touch event listener
 local function myTouchListener( event )
        if event.phase == "began" then
            print( "Touched!" )
        elseif event.phase == "moved" then
            print( "Touches Moved!" )
        elseif event.phase == "ended" then
            print( "Touches Ended!" )
        end
    end

--Adding an event listener for touch
Runtime:addEventListener( "touch", myTouchListener )

:)

How to add label/text to image in Corona ?

Hi,

For adding a label/text to an image in Corona, using Lua, use the following code,


local urGroup = display.newGroup()

local urImage = display.newImageRect('sample.png',70,70)
urImage.x = 150
urImage.y = 250

local urText = display.newText( "SampleText", 0, 0, "Helvetica", 22 )
urText:setTextColor( 0, 0, 0, 255 )

-- insert items into group, in the order you want them displayed:
urGroup:insert( urImage )
urGroup:insert( urText )

:)