Skip to content

Instantly share code, notes, and snippets.

@jstrahan
Last active April 30, 2017 01:42
Show Gist options
  • Save jstrahan/9451285 to your computer and use it in GitHub Desktop.
Save jstrahan/9451285 to your computer and use it in GitHub Desktop.
typeExtend
-----------------------------------------------------------------
-- copyright © 2014 Jeff Strahan --
-- Permission is granted to use this code --
-- in any of you own projects free of charge --
-- For more modules, templates, tutorials, and snippets --
-- visit http://www.j-strahan.com --
-----------------------------------------------------------------
-- --
-- type() returns the type of an object, (nil, number, string, --
-- boolean, table, function, thread, userdata ) as defined by --
-- lua. Have you ever wished you could detect a broader range --
-- of object types. With _typeExtend now you can detect if the --
-- object is a sound, stream, circle, rect, roundedRect, text, --
-- embossedText,image,imageRect,timer, file, container, group, --
-- line, polygon, sprite, transition, or snapshot. All that’s --
-- needed is 1 line of code to set it up. then use the type() --
-- command as usual and it will return the type of object --
-- supplied to the command. --
-- --
----------------------------------------------------------------
local oldType = type type = function(v) local function newType(v)
return ( oldType(v) == 'table' and v._type ~= nil and
oldType(v._type() ) == 'string' ) and v._type() or nil end
local options = { newType, io.type, oldType } for _,get in
pairs (options)do local t = get(v) if t then return t end end end
local temp = { sound = audio.loadSound,stream = audio.loadStream,
circle = display.newCircle, container = display.newContainer,
embossedText = display.newEmbossedText, group = display.newGroup,
image = display.newImage, imageRect = display.newImageRect,
line = display.newLine, polygon = display.newPolygon, rect =
display.newRect, roundedRect = display.newRoundedRect, snapshot =
display.newSnapshot,sprite=display.newSprite,text=display.newText
,timer = timer.performWithDelay, transitionTo = transition.to,
transitionFrom = transition.from } local common = function
(object, ... )local object = object local new = temp[object](...)
new._type = function( ) return object end return new end
function transition.to(...)return common('transitionTo',...)end
function timer.performWithDelay(...)return common('timer',...)end
function audio.loadStream(...) return common( 'stream' ,...)end
function audio.loadSound( ... ) return common( 'sound' , ... )end
function display.newCircle(... ) return common( 'circle', ...)end
function display.newGroup( ... ) return common( 'group' ,...)end
function display.newImage(...) return common( 'image' ,... ) end
function display.newLine(... ) return common( 'line' , ... ) end
function display.newPolygon(...) return common( 'polygon',...)end
function display.newRect( ... ) return common( 'rect' , ...) end
function display.newSnapshot(...)return common('snapshot',...)end
function display.newSprite( ... ) return common('sprite',...)end
function display.newText( ... ) return common( 'text' , ...)end
function transition.from(...)return common('transitionFrom',...)end
function display.newContainer(...)return common('container',...)end
function display.newImageRect(...)return common('imageRect',...)end
function display.newRoundedRect(...)return common('roundedRect',...)end
function display.newEmbossedText(...)return common('embossedText',...)end
require'_typeExtend'
local c1 = display.newCircle(160,200,20)
local r1 = display.newRect( 160, 300, 40, 40 )
local t1 = transition.to( c1, {time = 2000, x = 0} )
local t2 = timer.performWithDelay( 3000, function() end )
local f1 = io.open( system.pathForFile( "data.txt", system.DocumentsDirectory ), "a" )
print(type(c1))
print(type(r1))
print(type(t1))
print(type(t2))
print(type(f1))
c1:addEventListener( "touch", function() print('touched') end )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment