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
ssk.misc.addPhysicsDrag( obj [ , params ] ] )
- Add smart physics drag listener toobj
with optional parameters. Dragging is accomplished with a touch joint.obj
- The display object to attach 'smart' drag handler to.params
({}
) - Optional named-parameters table.force
(10
) -maxForce
value to use for touch joint.fromCenter
(true
) - If this optional parameter is set totrue
, the touch joint is attached to the center of the object, otherwise it is attached where the touch begins.toFront
(false
) - If this optional parameter is set totrue
raise object to top of its group when the touch begins.listener
- Optional parameter to specify a simplified touch listener. Simplified means: SSK2 takes care of multi-touch and focus handling for you.retval
(false
) - By default, the touch listener will returnfalse
. You can change that with this parameter.
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
ssk.misc.addSmartDrag( obj [ , params ] ] )
- Add smart drag listener toobj
with optional parameters.obj
- The display object to attach 'smart' drag handler to.params
({}
) - Optional named-parameters table.toFront
(false
) - If this optional parameter is set totrue
raise object to top of its group when the touch begins.listener
- Optional parameter to specify a simplified touch listener. Simplified means: SSK2 takes care of multi-touch and focus handling for you.retval
(false
) - By default, the touch listener will returnfalse
. You can change that with this parameter.
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
ssk.misc.addSmartTouch( obj [ , params ] ] )
- Add smart touch listener toobj
with optional parameters.obj
- The display object to attach 'smart' touch to.params
({}
) - Optional named-parameters table.toFront
(false
) - If this optional parameter is set totrue
raise object to top of its group when the touch begins.listener
- Optional parameter to specify a simplified touch listener. Simplified means: SSK2 takes care of multi-touch and focus handling for you.retval
(false
) - By default, the touch listener will returnfalse
. You can change that with this parameter.
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
ssk.misc.blockTouchesForDuration( [ duration [ , subtle ] ] )
- Creates a temporary display object that will block all touches forduration
(1000
).- By default, the blocker is black and up-fades from 0% to 50% opaque over 350 ms. However, if you want to hide this effect, set subtle to
true
.
- By default, the blocker is black and up-fades from 0% to 50% opaque over 350 ms. However, if you want to hide this effect, set subtle to
countLocals
ssk.misc.countLocals()
- This debug feature prints out the number of local variables that are in scope at the line where you call the function.- Lua has a limit of 200 locals in scope at any one time. This function lets you check for how close you are.
- Only works in simulator since it requires the 'debug' library which is not included on devices.
createPieChart
Inspired by free pie chart module from Rag Dog Studios.
ssk.misc.createPieChart( group, x, y, params )
- Creates a (circular or other) pie chart object.group
- Display group to insert pie-chart object in.x
,y
- < x, y > position of object center.params
- Table of parameters controlling pie-chart visuals.slices
- Table of slice definitions. See Slice Definitions below.mask
(mask.png
) - Path of 'mask' image to use if masking the pie chart.- Tip: You can generate masks with
ssk.misc.genCircleMask()
or create them with an art program.
- Tip: You can generate masks with
maskBase
(system.ResourceDiectory
) - Base directory for mask image.radius
(50
) - Radius of pie-chart. Specify this orsize
, not both.size
(100
) - Size of pie-chart. Specify this orradius
, not both.maskSize
(size
) - Width and height of mask.maskWidth
(size
) - Width of mask (overrides maskSize).maskHeight
(size
) - Height of mask (overrides maskSize).altStrokeWidth
(2
) - Width of stroke used for wedges.altStrokeColor
(slice color) - Color to use for wedge stroke. Defaults to current wedge color if not specified.noMask
(nil
) - Set totrue
to disble masking.
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
ssk.misc.easyAlert( group, x, y, params )
- A simplified 'dialog' builder that uses native.showAlert() to create a pop-up dialog.title
- Text at top of dialog box.msg
- Body of message. Multiple lines and line-breaks (\n
) allowed.buttons
- Table of buttons and optional functions they call when pressed.
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
ssk.misc.easyBlur( [ group, [ time, [ color, [ params ] ] ] )
- Create a display object that is a blurred image of what was on the screen when this function was called.group
(display.currentStage
) - Insert blur object into this group.time
(0
) - Time it takes (in millseconds) for the blur to fade in fromalpha == 0
.color
({0.5,0.5,0.5}
) - Shade blur object with this fill color.params
({ touchEn = true }
) - Table of additional options.touchEn
- Iftrue
, touching the screen will fade the blur object our overtime
then delete it.onComplete( obj )
- Reference to custom function that is called when blur is about to be removed.- blurGaussian properties. The blur uses this filter and you can adjust all of its properties if you want.
easyRemoteImage
ssk.misc.easyRemoteImage( curImg, fileName, imageURL, baseDirectory )
- With this function you can fill an existing display object with a remote image. You simply call this function and when the download is complete, the existingcurImg
object will be filled with the remote image file.curImg
- Display object to be filled with remote image.- Tip: I usually fill this object with a local placeholder image so it looks professional till the image is received.
fileName
- Name to save image in.imageURL
- Web path to image.baseDirectory
(system.TemporaryDirectory
) - Base type of directory.
easyShake
ssk.misc.easyShake( [ obj [, time [, amplitude [, delay ]]]] )
- Shake any object of the entire screen. Nice juicy effect!obj
(display.currentStage
) - Object or group to shake.amplitude
(100
) - How much to shake.time
(1000
) - How long to shake for.delay
(nil
) - Time to wait before starting shake.- Credit: Forum Post - Simple shake easing code and demo (external link)
easyUnderline
ssk.misc.easyUnderline( obj, [ color, [ strokeWidth, [ extraWidth, [ yOffset ]]]] )
- Draw a line under any object. I use this for text objects mostly.obj
- Object to 'underline'. Line length is automatically as wide as this object.color
(_W_
) - Color of line.strokeWidth
(1
) - Stroke width of line.extraWidth
(0
) - Make the line longer by this many pixels.yOffset
(0
) - Adjust the calculated y-position by this many pixels. By default, the line is aligned to the bottom edge of the display objectobj
.
fitText
ssk.misc.fitText( obj, origText, maxWidth )
- Reduces the length of a string till the text objectobj
fitsmaxWidth
(pixels).origText
is the initial string value ofobj
.
genCircleMask
Inspired by free pie chart module from Rag Dog Studios.
ssk.misc.genCircleMask( size, name )
- Generates a circle mask and saves it to the system.DocumentsDirectory.size
- Size of mask.- Rounds up to next power of 2.
- Can't be larger than largest dimension of screen.
name
- Name of file to save mask in.- mask.png, mask32.png, mask.jpg, etc.
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
ssk.misc.getImageSize ( path [ , basePath ] )
- Returnswidth
,height
of an image.path
- File name path including name of file.basePath
(system.ResourceDirectory
) - Base type of file path.
isConnectedToWWW
ssk.misc.isConnectedToWWW( [url] )
- Checks to see ifurl
can be reached. Default is"www.google.com"
isValidEmail
ssk.misc.isValidEmail ( val )
- Returns true ifval
seems like a valid email address.
Edges and Centers
Tip: The following x-/y- position finders do not care what the anchors are.
ssk.misc.oBottom( obj )
- Returns pixel y-position objectobj
bottom.ssk.misc.oHorizCenter( obj )
- Returns pixel x-position of objectobj
center.ssk.misc.oLeft( obj )
- Returns pixel x-position of objectobj
left.ssk.misc.oRight( obj )
- Returns pixel x-position of objectobj
right.ssk.misc.oTop( obj )
- Returns pixel y-position of objectobj
top.ssk.misc.oVertCenter( obj )
- Returns pixel y-position of objectobj
center.
pingPong
This function allow you to apply an non-ending ping-pong motion to an object using the transition functionality in Corona.
ssk.misc.pingPong( obj, [ params ] )
- Apply continous ping-pong transitions to object.obj
- Object to ping-pong.params
- Table of options controlling the rotate:ping
({ x = obj.x - 100 }
) - Ping transition definition.pong
({ x = obj.x + 100 }
) - Pong transition definition.tag
- Custom transition tag for ping-pong (allows you to pause/resume).firstTime
- This optional valuea allows you to override the very first transition duration.time
- Common ping/pong transition duration.- Setting time in
ping
orpong
definition will override this for that transition.
- Setting time in
delay
- Common ping/pong wait duration.- Setting delay in
ping
orpong
definition will override this for that transition.
- Setting delay in
transition
- Common ping/pong easing.- Setting transition in
ping
orpong
definition will override this for that transition.
- Setting transition in
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
ssk.misc.rotateAbout( obj, [ x, [ y, [ params ]]] )
- Rotate objectobj
about the point <x
,y
>.obj
- Object to rotate about a point.x
,y
(centerX
,centerY
) - The point.params
- Table of options controlling the rotate:radius
(50
) - Radius of rotation path.endRadius
- If specified, radius of rotation transitions to this over time.startA
(0
) - Start at this rotation angle, where0
is up,90
is right, etc.endA
(startA + 360
) - End at this rotation angle.delay
(0
) - Wait this long to start rotating.radiusDelay
(delay
) - Alternate delay for radius change.time
(1000
) - Take this long to rotate.radius
(time
) - Alternate time for radius change.myEasing
(easing.linear
) - Use this transition easing.debugEn
(false
) - Draw circles along path as rotation occurs.onComplete( obj )
(nil
) - Call this function when the rotation completes.
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
ssk.misc.secondsToTimer( [ seconds, [ version ] ] )
- Convertsseconds
into one of three different time formats.seconds
(0
) - Count of seconds to convert to a timer format.seconds
(1
) - Timer format to produce.1
- Return string with this format:"minutes:seconds"
2
- Return string with this format:"hours:minutes.seconds"
3
- Return four strings:"days"
,"hours"
,"minutes"
,"seconds"
Copyright © Roaming Gamer, LLC. 2008-2016; All Rights Reserved