[lua]
local breakSprite = function (params) – params table is passed to the function
local temp = display.newImage(params.image) – temporarily load our sprite using newImage
– as we don’t specify a size it will use its actual size, which we can then measure
local pieceSize = params.pieceSize – get the size of each piece from params table
local force = params.force – get the maximum force to apply to each piece
local density = params.density – get the density to give each piece
local px = params.x – get the x position to place the sprite before it explodes
local py = params.y – get the y position to place the sprite before it explodes
local options = { sheetContentWidth = temp.width, sheetContentHeight = temp.height, frames = {} }
– set up a table to pass to the .newImageSheet function.
– this contains the size of the imageSheet, which we get from the height/width of the temp image
– we loaded earlier. Set up a blank frames table to hold the frames data.
display.remove(temp) – remove the temp image as we no longer need it
temp = nil – nil the reference
local piecesX = options.sheetContentWidth/pieceSize – calculate the number of pieces on the X axis
local piecesY = options.sheetContentHeight/pieceSize – calculate the number of pieces on the Y axis
– we are splitting our image into a grid of 2x2 pieces
for a = 0, piecesX -1, 1 do – loop through the columns
for b = 0, piecesY -1, 1 do – loop through the rows
options.frames[b-1+(a\*piecesY)] = { – for this frame
xPos = a – remember its X position in the grid
yPos = b, – set its Y position in the grid
x=a*pieceSize, – set the top left position to start grabbing pixels
y = b*pieceSize, – set the top left position to start grabbing pixels
width = pieceSize, – set number of pixels across to grab for this frame
height = pieceSize} – set number of pixels down to grab for this frame
end
end
local sprites = graphics.newImageSheet(params.image, options)
– create a Corona image sheet using the image and the options we have set
local g = display.newGroup() – create a display group to hold all the pieces
for a = 1, #options.frames, 1 do – loop through all the frames
local i = display.newSprite(sprites, { name = “piece”, start = a, count = 1, time = 1})
– create a new 1 frame sprite sequence for each piece, named ‘piece’
i.x = options.frames[a].xPos * pieceSize – position the piece in its correct position
i.y = options.frames[a].yPos *pieceSize – position the piece in its correct position
i:setSequence(“piece”) – set the sprite sequence (probably redundant…)
i:play() – set the sprite sequence to play (probably redundant…)
g:insert(i) – insert this piece into the display group
physics.addBody(i, “static”, {density = density, bounce = bounce})
– give the piece a physics body. make it static so the pieces don’t fall until we want
local delayIt = function ()
i.bodyType = “dynamic” – change to dynamic so gravity will now apply
i:applyForce(math.random(-force, force),math.random(-force,force),i.x, i.y)
– give each piece a random force so the sprite appears to explode
end
timer.performWithDelay(params.delay, delayIt) – wait for the delay set in the params table
end
g.anchorX = 0.5 – give the group a central anchor point
g.anchorY = 0.5 – give the group a central anchor point
g.x = px – position the group where we specified in params table
g.y = py – position the group where we specified in params table
return g – returns the exploding sprite to a local variable
– we can then access it after so we can remove it, move it, hide it etc
end
[/lua]
[lua]
local sonic = breakSprite( – create an exploding sprite called sonic
{pieceSize = 2, – the size of each piece (i.e. 2x2 pixels)
image = “sonic.png”, – the filename of the image to explode
delay = 1000, – the delay before the image explodes, set to 1 for instantaneous
density = 2, – the density of each piece
bounce = 0.2, – the bounch of each piece
force = 2, – the maximum force applied to each piece (more density or bigger pieces = more force required)
x = 240, – the X position on the screen to place the sprite before it explodes
y = 160}) – the Y position on the screen to place the sprite before it explodes
[/lua]