Miscellaneous (ssk.misc.*)

This is a grab bag of functions that simply didn't fit anywhere else.

Having said that, I find many of these functions to be indispensable and I think you will too.

addPhysicsDrag addSmartDrag addSmartTouch
blockTouchesForDuration countLocals createPieChart
easyAlert easyBlur easyRemoteImage
easyShake easyUnderline fitText
genCircleMask getImageSize isConnectedToWWW
isValidEmail oBottom oHorizCenter
oLeft oRight oTop
oVertCenter pingPong rotateAbout
secondsToTimer

addPhysicsDrag

Note: This requires that the object have a physics body!


local physics = require "physics"

physics.start()

physics.setGravity(0,0)

local tmp = ssk.display.newCircle( nil, centerX, centerY, { fill = _Y_ }, {}  )

ssk.misc.addPhysicsDrag( obj, { toFront = true, retval = true, fromCenter = false } )

addSmartDrag


local function onDragged( event )
   print(" Dragging ", event.obj.name, event.dx, event.dy )
end; listen( "onDragged", onDragged )

local function onDropped( event )
   print(" Dropped ", event.obj.name, " @ " event.x, event.y )
   display.remove(event.obj)
end; listen( "onDropped", onDropped )


local obj1 = display.newCircle( 100, 100, 40 )
obj1.name = "bob"

local obj2 = display.newCircle( 200, 100, 40 )
obj2.name = "sue"

ssk.misc.addSmartDrag( obj1, { toFront = true, retval = true } )

ssk.misc.addSmartDrag( obj2, { toFront = true, retval = true } )

addSmartTouch


local function onTouch( self, event )
   if( event.phase == "ended" ) then
      print("Done")
      display.remove(self)
   end
end

local obj = display.newCircle( 100, 100, 40 )

ssk.misc.addSmartTouch( obj, { toFront = true, listener = onTouch, retval = true } )

blockTouchesForDuration

countLocals

createPieChart

Inspired by free pie chart module from Rag Dog Studios.

Pie Charts


local params = 
{
    slices = 
    {
        { degrees = 120, color = _R_},
        { degrees = 120, color = _G_},
        { degrees = 120, color = _B_},
    },
    mask = "mask64.png",
    size = 64,
    maskWidth = 64,
    maskHeight = 64,
}

local pie = ssk.misc.createPieChart( nil, centerX, centerY, params )

Slice Definitions

There are two ways to specify slices, using percentages or degrees

-- Percentages
slices = 
{
    { percentage = 100/3, color = _R_},
    { percentage = 100/3, color = _G_},
    { percentage = 100/3, color = _B_},
},

-- Degrees
slices = 
{
    { degrees = 120, color = _R_},
    { degrees = 120, color = _G_},
    { degrees = 120, color = _B_},
},

easyAlert


local function doDelete()
    -- Delete the player.
end

easyAlert( "Delete Player", 
            "Are you sure you want to delete this player?",
             { { "Yes!", doDelete }, {"Never Mind", nil } } )

easyBlur

easyRemoteImage

easyShake

easyUnderline

fitText

genCircleMask

Inspired by free pie chart module from Rag Dog Studios.

Note 1: If the device resolution does not match that specified in config.lua, the mask may not be the correct size and/or may be invalid.

Note 2: This is meant to be used during development, not production. See the code in ssk/misc.lua for more details on this.

getImageSize

isConnectedToWWW

isValidEmail

Edges and Centers

Tip: The following x-/y- position finders do not care what the anchors are.

pingPong

This function allow you to apply an non-ending ping-pong motion to an object using the transition functionality in Corona.

pingPong Definitions

ping and pong definitions can include any params legal for a transition.to().


local ping = { x = tmp.x - 200, myTime = 5000, myDelay = 500, transition = easing.outBounce }

local pong = { x = tmp.x + 200, myTime = 3000, myDelay = 0, transition = easing.outElastic }

pingPong Example


-- 1 Basic Ping Pong (default values)
--
local tmp = newImageRect( group, centerX, centerY - 240, "images/rg256.png", { size = 40 } )

ssk.misc.pingPong( tmp )

-- 2 Custom Ping Pong (x-only)
--
local tmp = newImageRect( group, centerX, centerY - 180, "images/rg256.png", { size = 40 } )

local ping = { x = tmp.x - 200, myTime = 5000, myDelay = 500, transition = easing.outBounce }

local pong = { x = tmp.x + 200, myTime = 3000, myDelay = 0, transition = easing.outElastic }

ssk.misc.pingPong( tmp, { ping = ping, pong = pong } )

end

rotateAbout

Rotate About w/ Radius Change

local axis2 = newCircle( group, centerX + 150, centerY - 100, { fill = _O_ })

for i = 1, 10 do
    local tmp = newRect( group, axis2.x, axis2.y, { fill = randomColor(), size = 10 } )
    local doSpin
    ssk.misc.rotateAbout( tmp, axis2.x, axis2.y, 
                          { startA = i * 36, 
                            endA = i * 36 + 1800,
                            time = 1000, radius = 125, 
                            endRadius = 10,
                            onComplete = display.remove } )
end

Rotate About w/ Radius & Alternate radiusTime

This example changes the radius faster than the rotatate time.

-- Inspired by this thread:
-- https://forums.coronalabs.com/topic/67561-have-multiple-bullets-fire-inward-in-a-circle-formation-ssk2/
local axis3 = newCircle( group, centerX, centerY + 150, { fill = _R_ })

local doBullets
doBullets = function( axis, delay, count )
   for i = 1, 10 do
    local tmp = newRect( group, axis3.x, axis3.y, { fill = _R_, size = 10 } )
    local angle = i * 36
    ssk.misc.rotateAbout( tmp, axis3.x, axis3.y, 
                          { startA = angle, endA = angle + 360 + 36,
                            time = 2000, radius = 125, 
                            endRadius = 10, radiusTime = 750 } )

      tmp.onComplete = function( self )
        ignore("enterFrame", self)
        display.remove( self )
      end
   end

   if( count ) then 
    count = count - 1
    if( count <= 0 ) then return end
    timer.performWithDelay( delay, function() doBullets(axis, delay, count) end )
   end
end
doBullets( axis3, 600, 3 )


secondsToTimer


RoamingGamer Copyright © Roaming Gamer, LLC. 2008-2016; All Rights Reserved