Friday, 23 August 2013

Different device Orientation in Corona SDK

In this tutorial, we will teach you: how to setup different supported orientations
for your Corona SDK application. These tricks are done in the build.settings file.
It will setup app orientation in relation to the device's physical orientation.
This also includes auto orientation triggered by the accelerometer, if the
device is flipped/rotated during runtime.

By this, you can set the orientation for:
    1) Launch image(splash screen).
    2) Native UI elements(like alerts, native keyboard,etc).
    3) The Corona display stage.
   
We will show you some of the examples in different orientations.

Example 1: Only landscapeLeft
 

    settings =
        {
            orientation =
            {
                default = "landscapeRight",
            },
        }



 
Example 2: Both landscapeLeft and landscapeRight


    settings =
    {
        orientation =
        {
            default = "landscapeRight",
            supported = { "landscapeLeft", "landscapeRight" },
        },
    }


   
Example 3: Only portrait    
 


    settings =
        {
            orientation =
            {
                default = "portrait",
            },
        }




Example 4: Both portrait and portraitUpsideDown


    settings =
    {
        orientation =
        {
            default = "portrait",
            supported = { "portrait", "portraitUpsideDown" },
        },
    }




Sunday, 18 August 2013

How to create widget button in corona DSK

In this tutorial, we will teach you the method of creating Widget button in your Corona SDK application.

First of all, you need to download the following 2 images and keep it with your 'main.lua'.


myButton_normal.png
 

myButton_press.png



Name the first button as: myButton_normal.png
and name the second as: myButton_press.png

Now we can start coding. Lets open main.lua. In the top of your code, require 'widget' as follows:


    local widget = require( "widget" )  


Now create a label to display the button events:
  
    local myText = display.newText( "Click on the buttons.", 0, 0,nil, 18 )
    myText.x, myText.y = display.contentCenterX, 70


Now we have to create the functions triggered by the buttons:

    local button1Press = function( event )
        myText.text = "Button 1 pressed"
    end

    local button1Release = function( event )
        myText.text = "Button 1 released"
    end

    local buttonHandler = function( event )
        myText.text = "id = " .. event.target.id .. ", phase = " .. event.phase
    end


Then just create first button as follows. This button has individual press and release functions.


    local button1 = widget.newButton{
        defaultFile = "myButton_normal.png",
        overFile = "myButton_press.png",
        onPress = button1Press,
        onRelease = button1Release,
        label = "Button 1 Label",
        emboss = true
    }

    button1.x = 160; button1.y = 160


Now we are going to create the second button. This has a single event handler function.

    
    local button2 = widget.newButton{
        defaultFile = "myButton_normal.png",
        overFile = "myButton_press.png",
        onEvent = buttonHandler,
        label = "Button 2 Label",
        id="myButton_2",
        emboss = true
    }
    button2.x = 160; button2.y = 240


 Now, save and run your application. You can see the event label and two buttons. Perform Touch, Drag and Release on the buttons and check it with the label.

Note: Please update your corona SDK version to include all new widget features.




How to get the device information using corona SDK

In some section of coding, you may need be to find out the current device specifications that you are using to run your corona applications. This article is about how to find out basic device information programmatically for your application using Corona SDK. Lets look at the following sample, it can help you for sure:


 To get the device Name:
        
  print("Name:"..system.getInfo( "name" ))


To get the device Model:

    print("Model:"..system.getInfo( "model" ))


To get the device Environment:

    print("Env:"..system.getInfo( "environment" ))


To get the Platform Name:

    print("Plat:"..system.getInfo( "platformName" ))


To get the Platform Version:

    print("Plat. ver:"..system.getInfo( "platformVersion" ))


To get the device Version:

    print("Ver:"..system.getInfo( "version" ))

To get the device Build:

    print("Build:"..system.getInfo( "build" ))

To get the Device ID:

    print("Dev ID:"..system.getInfo( "deviceID" ))


To get the device Language Code:

    print("Language code:"..system.getPreference( "ui", "language" ))


To get the Country:
   
  print("Country:"..system.getPreference( "locale", "country" ))


To get the Identifier:

    print("Identifier:"..system.getPreference( "locale", "identifier" ))


To get the device Language:
    
  print("Language:"..system.getPreference( "locale", "language" ))





Thursday, 15 August 2013

How to create global objects in your corona SDK game

In this tutorial, we will teach you how to make global objects with it's own listeners and properties, and are accessible to any class in your game with corona SDK. Here the director class is used to change from scene to scene.

Here I have my .lua  files: main, status and menu. First, lets start with the main.lua. Do the followings in your main:

main.lua
                                                                                 


     local director=require("director")
     local maingroup=display.newGroup()
     maingroup:insert(director.directorView)
     director:changeScene("menu")
     return maingroup 
                                                                                   

Here, we can see that the director class is loaded, and a main displayGroup is created. Next we need to create a class which contains our global object. I am naming that class as 'status.lua'. Here we can create the object method with its certain object with listener and properties and while calling the method, you can return the object to any scene in your game/application. Just look at the status.lua class:

status.lua
                                                                                

    local function myObject(group,x,y,imagePath)

      local image = display.newImage( imagePath )
      image.x, image.y = x, y
      group:insert( image )

      -- We can even add properties as below--
      transition.to(image, {time=1000, x=160, y=300, transition=easing.inOutQuad}) 

      -- Adding Listener --
      image:addEventListener("touch", function() print("imageClicked") end )
    end

    local status = { myObject = myObject }

    return status
                                                                               

Now we can create our menu scene, which is the menu.lua. Here we have to require the object page ('status') and have to call the object method in the page to get the object in current scene, as follows:

menu.lua
                                                                                


module(...,package.seeall)

function new()

    -- require object page --
    local status = require "status"

    -- create a display group --
    local localGroup = display.newGroup()

    -- call object --
    status.myObject(localGroup, 160, 80, "muImage.png")

 return localGroup
end
                                                                               

Now save your files and run the app in the simulator/device to see what happens...

Wednesday, 7 August 2013

How to create a native alert view using corona SDK



The below function defines the two alert button ('Learn More' & 'OK') actions:

local function onClick(e) 
    if e.action == "clicked" then 
        if e.index == 1 then 
            -- Do something
        elseif e.index == 2 then 
            system.openURL( "http://devblogdk.blogspot.in/" ) 
        end 
    end 
end 

The below code generates the native alertview with title, subject and two buttons:

local alert = native.showAlert("Welcome", "Development Tutorials on DevBlogDK", {"OK", "Learn More"}, onClick)

How to get the largest integer smaller than or equal to a given number in corona SDK

We can find out the largest integer smaller than or equal to a given number using math.floor(). 

Syntax:
   
    math.floor()
   
Example:

    print(math.floor(0.5))    --->     0
    print(math.floor(-0.5))  --->     -1

How to generate a random number in corona SDK

We can use math.random() for the purpose of generating random numbers in corona SDK.
There are 3 main types for this call. They are:

1) Witout any argument:
        math.random()
        -- This will return a pseudo random number in the range of [0,1)
   
2) With one argument:
        math.random(a)
        -- This will return a pseudo random number in the range of [0,a]
   
        eg: print(math.random(5))  ----> return a number between 1 and 5 (both inclusive)
   
3) With two arguments:
        math.random(a,b)
        -- This will return a pseudo random number in the range of [a,b]
   
        eg: print(math.random(2,10))  ----> return a number between 2 and 10 (both inclusive)