local sprite = app.activeSprite
if not sprite then
return app.alert(“There is no sprite to work on!”)
end
– Number of frames for the explosion animation
local numFrames = 10
– Create new frames for the explosion
for i = 1, numFrames do
sprite:newFrame()
end
– Function to randomly move pixels outward
local function randomExplosion(cel, frame)
local image = cel.image:clone()
local x, y = cel.position.x, cel.position.y
local displacement = frame * 2 – Increasing displacement over time
for pixel in image:pixels() do
if pixel() ~= 0 then -- Ignore transparent pixels
-- Randomize the direction of the explosion
local dx = math.random(-displacement, displacement)
local dy = math.random(-displacement, displacement)
-- Calculate new position for the pixel
local newX = pixel.x + dx
local newY = pixel.y + dy
-- Boundaries check
if newX >= 0 and newX < image.width and newY >= 0 and newY < image.height then
image:drawPixel(newX, newY, pixel()) -- Move pixel to new location
image:drawPixel(pixel.x, pixel.y, 0) -- Clear old location
end
end
end
cel.image = image
end
– Apply the explosion effect frame by frame
for frame = 1, numFrames do
local cel = sprite.cels[1] – Start with the original cel
-- Duplicate the image into each frame
app.command.CopyCel{ fromFrame = 1, toFrame = frame }
-- Apply the explosion effect to each frame
randomExplosion(sprite.cels[frame], frame)
end
– Refresh the UI to show changes
app.refresh()