Skip to content

Instantly share code, notes, and snippets.

@walterlua
walterlua / table_indexOf.lua
Created May 18, 2011 07:38
Implementation of JavaScript array.indexOf() in Lua. Also, adds the function to Lua's table library
-- table.indexOf( array, object ) returns the index
-- of object in array. Returns 'nil' if not in array.
table.indexOf = function( t, object )
local result
if "table" == type( t ) then
for i=1,#t do
if object == t[i] then
result = i
break
@walterlua
walterlua / math_round.lua
Created May 18, 2011 07:45
Implementation of JavaScript math.round() in Lua. Also, adds the function to Lua's math library
-- math.round( num ) rounds num to the nearest integer following the
-- same rules as the JavaScript version, i.e. if the fractional
-- portion of number is .5 or greater, the argument is rounded to the
-- next higher integer. If the fractional portion of number is less
-- than .5, the argument is rounded to the next lower integer.
math.round = function(num)
return math.floor(num+.5)
end
-- Example Usage:
@walterlua
walterlua / table_copy.lua
Created May 18, 2011 07:50
Implementation of JavaScript array.concat() in Lua. Also, adds the function to Lua's table library
-- table.copy( array, ... ) returns a shallow copy of array.
-- A variable number of additional arrays can be passed in as
-- optional arguments. If an array has a hole (a nil entry),
-- copying in a given source array stops at the last consecutive
-- item prior to the hole.
--
-- Note: In Lua, the function table.concat() is equivalent
-- to JavaScript's array.join(). Hence, the following function
-- is called copy().
table.copy = function( t, ... )
@walterlua
walterlua / math_round_test.lua
Created May 18, 2011 07:53
Test case for math.round() function
math.round = function(num)
return math.floor(num+.5)
end
local function testRound( num )
print( num, math.round( num ) )
end
testRound( 0.1 )
testRound( 0.5 )
@walterlua
walterlua / gist:978165
Created May 18, 2011 07:54
Output for math_round_test
0.1 0
0.5 1
8.9 9
-0.1 0
-0.5 0
-8.9 -9
@walterlua
walterlua / print2.lua
Created June 17, 2011 07:12
Utility method for printing the contents of a variable, especially tables
function print2( var, name )
if not name then name = "anonymous" end
if "table" ~= type( var ) then
print( name .. " = " .. tostring( var ) )
else
-- for tables, recurse through children
for k,v in pairs( var ) do
local child
if 1 == string.find( k, "%a[%w_]*" ) then
-- key can be accessed using dot syntax
@walterlua
walterlua / gist:1030996
Created June 17, 2011 07:13
print2 example
-- Example:
local t = { foo="bar", a=3, b=false, [" blah"] = "lah", ["12s{@#}$#)@*"]=3}
print2( t, "t" )
-- Output:
t.a = 3
t[" blah"] = lah
t.b = false
t["12s{@#}$#)@*"] = 3
t.foo = bar
@walterlua
walterlua / tinting.lua
Created September 7, 2011 22:34
Example showing image tinting based on modifying the HelloWorld sample in Corona SDK
local w,h = display.contentWidth*0.5, display.contentHeight*0.5
local halfW,halfH = 0.5*w, 0.5*h
local background = display.newImageRect( "world.jpg", w,h )
background:translate( halfW, halfH )
local dw = w + halfW
local dh = h + halfH
local background = display.newImageRect( "world.jpg", w,h )
background:setFillColor( 255, 255, 0 )
@walterlua
walterlua / gradient.lua
Created September 7, 2011 23:19
Gradient on text demo in Corona SDK
local myText = display.newText( "Hello, World!", 0, 0, native.systemFont, 40 )
myText.x = display.contentWidth * 0.5
myText.y = display.contentWidth * 0.25
local g = graphics.newGradient( { 255, 255, 0 }, { 200 }, "down" )
myText:setTextColor( g )
@walterlua
walterlua / gist:3726123
Created September 15, 2012 02:31
Corona iPhone 5 tall app detection
local isTall = ( "iPhone" == system.getInfo( "model" ) ) and ( display.pixelHeight > 960 )