Archive for the ‘Corona’ Category

corona sdk for iphone/Android

27
Apr

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

-- jsonFile() loads json file & returns contents as a string

local jsonFile = function( filename, base )

 

-- set default base dir if none specified

if not base then base = system.ResourceDirectory; end

 

-- create a file path for corona i/o

local path = system.pathForFile( filename, base )

 

-- will hold contents of file

local contents

-- io.open opens a file at path. returns nil if no file found

local file = io.open( path, "r" )

if file then

-- read all contents of file into a string

contents = file:read( "*a" )

io.close( file ) -- close the file after using it

end

 

return contents

end

27
Apr
local Chars = {}
for Loop = 0, 255 do
   Chars[Loop+1] = string.char(Loop)
end
local String = table.concat(Chars)

local Built = {['.'] = Chars}

local AddLookup = function(CharSet)
   local Substitute = string.gsub(String, '[^'..CharSet..']', '')
   local Lookup = {}
   for Loop = 1, string.len(Substitute) do
       Lookup[Loop] = string.sub(Substitute, Loop, Loop)
   end
   Built[CharSet] = Lookup

   return Lookup
end

function string.random(Length, CharSet)
   -- Length (number)
   -- CharSet (string, optional); e.g. %l%d for lower case letters and digits

   local CharSet = CharSet or '.'

   if CharSet == '' then
      return ''
   else
      local Result = {}
      local Lookup = Built[CharSet] or AddLookup(CharSet)
      local Range = table.getn(Lookup)

      for Loop = 1,Length do
         Result[Loop] = Lookup[math.random(1, Range)]
      end

      return table.concat(Result)
   end
end

26
Apr

For identifying whether a requested host is reachable via WiFi in Corona, use ‘event.isReachableViaWiFi’.

Syntax:

local result = event.isReachableViaWiFi

Example:

function MyNetworkReachabilityListener(event)
        print("IsReachableViaWiFi", event.isReachableViaWiFi)
end

Returns:
Boolean: True if reachable through WiFi, false otherwise.

26
Apr

To enable the GPS on Android devices, you must set the permission level in the build.settings file.

settings =
{
   androidPermissions =
   {
       "android.permission.ACCESS_FINE_LOCATION"
   },
}

26
Apr

For checking two numbers/strings , say v1 and v2 equals in corona, without invoking any metamethod, use ‘rawequal()’,

Syntax:

value = rawequal (v1, v2)

Example:

local v1 = 5
local v2 = 5
print( rawequal( v1, v2) ) -- true

Parameters:
v1, v2
Two values to be compared. Can be numbers or strings.

Returns:
Boolean: True if the two values are equal or false if they are not.

26
Apr

Use ‘audio.rewind()’, it rewinds audio to the beginning position on either an active channel or directly on the audio handle.

Syntax:

audio.rewind( [, audioHandle ] [, { channel=c } ] )

Example:

audio.rewind()  -- rewind all channels
audio.rewind( backgroundMusic ) -- rewind the audio handle
audio.rewind( { channel=1 } )  -- rewind channel 1

Parameters:
audioHandle

object: The audioHandle of the data you want to rewind. Best for audio loaded with audio.loadStream(). Don’t try using with the channel parameter in the same call.

channel
integer: The channel you want the rewind operation to apply to. Best for audio loaded with audio.loadSound(). Don’t try using with the audioHandle parameter in the same call.

Returns:
True on success or false

Remarks:
There are subtle behavior differences depending on whether you used audio.loadSound() or audio.loadStream() on what you are trying to rewind.

Audio loaded with audio.loadSound() may only seek using the channel parameter. You may not rewind using the audioHandle. This is because audio.loadSound() is optimized to share the audio data so you can play back multiple instances of the sound simultaneously (at different positions). Seeking (rewinding) the underlying data complicates this optimization.

In contrast, audio loaded with audio.loadStream() cannot be shared (you would load multiple instances of the same file if you needed multiple, simultaneous playback). So seeking the data does not cause a conflict. So generally you are expected to rewind using the audioHandle parameter for audio loaded with audio.loadStream(). But if you rewind streamed data using the channel parameter, it will automatically rewind as if you used the audioHandle parameter. So you are allowed to specify either parameter safely.

Also note that for files loaded with loadStream and are currently playing, you may not hear the audio immediate update until after the current buffer finishes playing. If you want seemingly instantaneous rewinding, you should stop the playback first using audio.stop(), rewind, then start playing.

25
Apr

Use ‘audio.isChannelPlaying()’ api for checking whether an audio channel is playing in Corona.
It returns true if the specified channel is currently playing.

Syntax:

audio.isChannelPlaying( channel )

Example:

local isChannel1Playing = audio.isChannelPlaying( 1 )
if isChannel1Playing then
    audio.pause( 1 )
end

Parameters:
channel
integer: The channel you want to know is playing or not.

Returns:
True for playing, false otherwise

25
Apr

‘easing.outQuad( )’ starts motion fast and then decelerates motion to a zero velocity as it executes.

Syntax:

transition.to(displayObject, { transition=easing.outQuad})
transition.from(displayObject, { transition=easing.outQuad})
transition.dissolve(displayObject, { transition=easing.outQuad})

Example:

local currentTarget = display.newCircle(100, 100, 100)
transition.to(currentTarget, { time=400, y=300, transition=easing.outQuad})

25
Apr

Description:

Moves the displayed map region to a new location, using the new center point but keeping the zoom level the same. The final parameter is an optional Boolean (default false) that determines whether the transition is animated or happens instantly.

Syntax:

myMap:setCenter( latitude, longitude, isAnimated )

25
Apr

Description:

Flushes the default output file. Equivalent to io.output():flush.

If io.output has not been changed from it’s default output (stdout), this will flush any io.write or print data to the Corona Terminal, Xcode Console, or consol.app.

Syntax:

io.flush()