Last active
May 8, 2018 05:52
-
-
Save HoraceBury/dd5e163a61458a1bcdbc36921cdf5bde to your computer and use it in GitHub Desktop.
Shadow library. Given a display group, this will ensure that non-physics-based shadows will be updated to track their parent, whether the parent is physics-based or not. The test sample demonstrates using physics to provide a shadow, as well.
This file contains hidden or 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 lib = {} | |
function lib.newShadowLayer( shadowgroup ) | |
local function refresh() | |
if (shadowgroup and shadowgroup.numChildren) then | |
for i=1, shadowgroup.numChildren do | |
local shadow = shadowgroup[i] | |
local object = shadowgroup[i].shadowparent | |
if (not shadow.isBodyActive) then | |
shadow.x, shadow.y, shadow.rotation = object.x, object.y, object.rotation | |
shadow.rotation = shadow.rotation | |
end | |
end | |
end | |
end | |
function shadowgroup:enterFrame() | |
timer.performWithDelay( 1, refresh ) | |
end | |
Runtime:addEventListener( "enterFrame", shadowgroup ) | |
function shadowgroup:finalize() | |
Runtime:removeEventListener( "enterFrame", shadowgroup ) | |
shadowgroup = nil | |
end | |
shadowgroup:addEventListener( "finalize" ) | |
return shadowgroup | |
end | |
return lib |
This file contains hidden or 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 function shadowLibTest() | |
local physics = require("physics") | |
physics.start() | |
physics.setGravity(0,0) | |
-- physics.setDrawMode("hybrid") | |
local shadowlib = require("shadowlib") | |
local shadows, objects = display.newGroup(), display.newGroup() | |
shadows.y = 7 | |
local shadA = display.newRoundedRect( shadows , 200,200 , 200,100 , 25 ) | |
local rectA = display.newRoundedRect( objects , 200,200 , 200,100 , 25 ) | |
shadA.shadowparent = rectA | |
local shadB = display.newRoundedRect( shadows , 200,500 , 200,100 , 25 ) | |
local rectB = display.newRoundedRect( objects , 200,500 , 200,100 , 25 ) | |
shadA.fill = {.7,.7,.7} | |
shadB.fill = {.7,.7,.7} | |
physics.addBody( rectB, "dynamic" ) | |
physics.addBody( shadB, "dynamic" ) | |
physics.newJoint( "weld", rectB, shadB, rectB.x, rectB.y ) | |
transition.to( rectA, { time=5000, iterations=0, rotation=rectA.rotation+360 } ) | |
rectB.angularVelocity = 200 | |
shadowlib.newShadowLayer( shadows ) | |
shadowlib.add( rectA, shadA ) | |
shadowlib.add( rectB, shadB ) | |
end | |
shadowLibTest() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment