Tuesday, 30 July 2013

How to load a remote image to your application screen using Corona SDK

 Is it possible to load a picture that is located on a server, and display it on your application screen using corona SDK..? Yes it is...
You can use the following code for achieving this:

local function networkListener( event )
        if ( event.isError ) then
                print ( "Network error - download failed" )
        else
                event.target.alpha = 0
                transition.to( event.target, { alpha = 1.0 } )
        end

        print ( "RESPONSE: " .. event.response )
end

display.loadRemoteImage( "http://goo.gl/FsRnwu", "GET", networkListener, "banner.png", system.TemporaryDirectory, 25, 50 )

Monday, 29 July 2013

How to launch camera in your iOS app using corona SDK

You may need to use the camera in several applications to take photos/videos. In corona SDK, you can launch device camera using the following code:

    local onComplete = function(event)
            local photo = event.target
            print( "photo w,h = " .. photo.width .. "," .. photo.height )
    end
    media.show( media.Camera, onComplete )

Sunday, 28 July 2013

How to create a widget button in Corona SDK

Creating a widget button in corona, with a button press effect and a function call is as simple as below:


-- First, require 'widget' --
local widget = require("widget")

-- Create the button --
local myButton = widget.newButton{
    default = "normalImage.png",
    over = "pressedImage.png",
    width = 200, height = 200,
    onRelease = myButtonClickFunction
}

-- Position the button --
myButton.x = display.contentWidth/2
myButton.y = display.contentHeight/2

-- Then create the function you need to trigger on the button press --

function myButtonClickFunction()
    print("myButton is pressed...")
end









Touch listener in Corona SDK

We can assign touch listener to an object in corona as follows:


-- Create the object --
local rect = display.newRect(100,100,50,50)
rect:setFillColor(255,0,0)

-- Create the touch function --
local function myTouchFunction(event)
    if(e.phase=="began")then
         print("Touch begins...")
    elseif(e.phase=="moved")then
         print("Touch moved...")
    elseif(e.phase=="ended")then
         print("Touch ended...")
    end
end

-- Create the listener to the object --
rect:addEventListener("touch",myTouchFunction)








Wednesday, 24 July 2013

How to capture whole screen in Corona SDK

You can use the following lines of code to capture the device screen in Corona SDK. This will save the screenshot as: Picture 1.png, Picture 2.png, etc in the sandbox, and will show it on the screen with half size(if you do not need this, you can remove it by calling captured_image:removeSelf() and captured_image=nil). It can save upto 10000 files.

Syntax:

    local function captureDeviceScreen()
       local captured_image = display.captureScreen( true )
       captured_image:scale(.5,.5)
       local alert = native.showAlert( "Success", "Captured Image is Saved to Library", { "OK" } )
    end
    Runtime:addEventListener("tap",captureDeviceScreen)


And if you want to save the image with desired name, then you can refer the following post:

How to mail a screen captured image using corona SDK



How to zoom in an object and zoom back to normal state in Corona SDK

In Cocos2d you can easily scale an object within two values. For example, you can scale an object to 0.2, and can restore it to 1, whenever you need. But in Corona, if you scale to an object to a value of 0.2, and try and rescale to 1, it keeps it as 0.2. Because, in corona SDK, when you scale an object to a value other than 1, the system will take the new scaled value as the fullscale of the object. So, here we will teach you a simple technique to scale/zoom in and out an object in corona SDK.

1: Create a scaleFactor :

               local scaleFactor = 0.2;

2: When you want to zoom out, do :

               myObject:scale(scaleFactor ,scaleFactor )

3: When you want to zoom in, do :

              myObject:scale(1/scaleFactor ,1/scaleFactor )


Auto-change the physics of an object (created using Physics Editor) after a change of it's size in corona SDK

If you are using physics editor for making physics objects in your corona game, then at some point you may doubted about how to scale the physics area according to the sprite scale values... But it is very simple.

For doing such alternation, there is scaleFactor provided. You can assign physics to the object with this scale factor, as below:

    local scaleFactor = 1.0
    local physicsData = (require "shapedefs").physicsData(scaleFactor)
    local shape = display.newImage("objectname.png")
    physics.addBody( shape, physicsData:get("objectname") )


And when you change the scaleFactor, the size of physics area will automatically change.

How to mail a screen captured image using corona SDK

For doing this, you have to:
  • First create a localgroup.
  • Then add the screen objects to that group.
  • Return the display group
  • Use display.save to save the entire group displayed.
  • Create mail option and add attachment image from baseDirectory
  • Call mail Popup
Example

-- creating the display group --
local localGroup = display.newGroup()  

-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,320,480)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)

local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)

-- Then do as follows --
local function takePhotoAndSendMail()
  -- take screen shot to baseDirectory --
  local baseDir = system.DocumentsDirectory
  display.save( localGroup, "myScreenshot.jpg", baseDir )

  -- Create mail options --
  local options =
  {
    to = { "devblogdk@gmail.com",},
    subject = "My Level",
    body = "Add this...",
    attachment =
    {
      { baseDir=system.DocumentsDirectory, filename="myScreenshot.jpg", 
        type="image" },
    },
  }

  -- Send mail --
  native.showPopup("mail", options)
end
rect:addEventListener("tap",takePhotoAndSendMail)