Just thought I would post this code and see what you guys think. It’s a test for implementing an explosion. Basically, once you’ve copied the sample code crate.png, crateB.png and rock.png images into the app, the white circles can be moved around by the touch event and the tap event will progressively switch their physics bodies on, small to large.
[lua]local physics = require(“physics”)
display.setStatusBar( display.HiddenStatusBar )
physics.start()
physics.setGravity( 0, 20 )
physics.setDrawMode( “normal” ) – normal, hybrid, debug
local room = display.newGroup()
local world = display.newGroup()
local bullets = {}
function makeWall( x, y, w, h )
local wall = display.newRect( 0, 0, w, h )
room:insert( wall )
wall.x, wall.y = x, y
physics.addBody( wall, “static”, { friction=1, bounce=.5, density=1 } )
return wall
end
function makeObject( img, x, y, r )
local obj = display.newImage( world, img, x, y )
if ® then
physics.addBody( obj, “dynamic”, { friction=1, bounce=.2, density=10, radius=r } )
else
physics.addBody( obj, “dynamic”, { friction=1, bounce=.2, density=10 } )
end
return obj
end
function makeBullet( x, y, r )
bullet = display.newCircle( x, y, r )
physics.addBody( bullet, “kinematic”, { friction=0, bounce=100, density=1, radius=r } )
bullet.alpha = .5
bullet.isBodyActive = false
return bullet
end
for i=1, 5 do
bullets[#bullets+1] = makeBullet( display.contentCenterX, display.contentCenterY, 40*i )
end
function tap( event )
print(‘on’)
for i=1, #bullets do
– first timer delays the physics effect of the circle
timer.performWithDelay(
i*20,
function()
– first timer makes the circle body active, so that as they turn on they progressively push objects away
bullets[i].isBodyActive = true
– second timer switches the circle off again
timer.performWithDelay(
100,
function()
bullets[i].isBodyActive = false
end,
1
)
end,
1
)
end
return true
end
function touch( event )
for i=1, #bullets do
bullets[i].x, bullets[i].y = event.x, event.y
end
return true
end
local roof = makeWall( display.contentCenterX, 0, display.contentWidth, 2 )
local left = makeWall( 0, display.contentCenterY, 2, display.contentHeight )
local right = makeWall( display.contentWidth, display.contentCenterY, 2, display.contentHeight )
local floor = makeWall( display.contentCenterX, display.contentHeight, display.contentWidth, 2 )
for i=1, 25 do
local t, x, y = math.random( 1, 3 ), math.random( 50, 300 ), math.random( 50, 300 )
if (t == 1) then
makeObject( “crate.png”, x, y )
elseif (t == 2) then
makeObject( “crateB.png”, x, y )
else
makeObject( “rock.png”, x, y, 35 )
end
end
Runtime:addEventListener( “tap”, tap )
Runtime:addEventListener( “touch”, touch )[/lua]
m [import]uid: 8271 topic_id: 7994 reply_id: 307994[/import]