shinu.s

shinu.s's page

09
May

We can define event listeners in three different ways in Corona, see the examples given below.

– METHOD 1:

    local function touchListener( event )
        ...
    end

    myImage:addEventListener( "touch", touchListener )

———————————————————————-

— METHOD 2:

    local function touchListener( self, event )

        -- In this example, "self" is the same as event.target
        ...
    end

    -- Notice the difference in assigning the event:
    myImage.touch = touchListener
    myImage:addEventListener( "touch", myImage )

———————————————————————-

— METHOD 3:

    function myImage:touch( event )

        -- Same as assigning a function to the .touch property of myImage
        -- This time, "self" exists, but does not need to be defined in
        -- the function's parameters (it's a Lua shortcut).
        ...
    end

    myImage:addEventListener( "touch", myImage )

08
May

For adding a Shaky3D effect on a cocos2D sprite, use the following sample code,

id shakyActn = [CCShaky3D actionWithRange:4 shakeZ:NO grid:ccg(15,10) duration:5];
[shakingSprite runAction: [CCRepeatForever actionWithAction: shakyActn]];

This lines of code will add a shaky action to shakingSprite.

02
May

For getting available font styles for a particular font in iPhone app,

NSLog(@"%@", [UIFont fontNamesForFamilyName:@"Helvetica Neue"]);

It prints all available font styles for Helvetica Neue, among them , one is HelveticaNeue-Medium

02
May

For checking whether plist does exist,

if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath] == 0) {
//Plist Exists
}

For checking it doesn’t exist,

if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath] != 0) {
//Plist didn't exist
}

since the code returns 0 when the file does exist.

02
May

For detecting when the user unlocked the iPhone from your application use,

-(void)applicationDidBecomeActive:(UIApplication *)application {

   //Your actions
}

in the appdelegate.m

02
May

For detecting iphone application entering background/sleep mode, use any of the following,

-(void)applicationWillResignActive:(UIApplication *)UIApplicationWillResignActiveNotification{

//Your actions
}

or

-(void)applicationDidEnterBackground:(UIApplication *)application {

//Your actions
}

Use these in the appdelegate.m

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.