This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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, ... ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0.1 0 | |
0.5 1 | |
8.9 9 | |
-0.1 0 | |
-0.5 0 | |
-8.9 -9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local isTall = ( "iPhone" == system.getInfo( "model" ) ) and ( display.pixelHeight > 960 ) |
OlderNewer