Now that we have some notion of how to combine the elements, we'll put them together in the actual code. Because we have two different images that we'll want layered together, the splatter object can't be just a single display object any more. We'll replace it with a group, layer on highlights, and add the impression of motion.
Make sure that the effects folder in your project contains the images blood.png
, splat.png
, and highlight.png
. Copy these from the version 1
subfolder of the Predation
directory in the project pack if needed.
Start by opening the file blast.lua
and perform the following steps:
local category = require "category"
local clip = graphics.newMask("effects/splat.png")
local function shrink(object)
return function (world, cause, x, y) local self = display.newGroup() world:insert(self) self.x, self.y = x, y self:toBack()
world:insert(self)
self.x, self.y = x, y
self:scale(25/32, 25/32)
self:toBack()
self:scale(25/32, 25/32) self.Blood = display.newImage(self, "effects/blood.png") self.Blood.x, self.Blood.y = 0, 0 self:toBack()
self.Blood = display.newImage(self, "effects/blood.png") self.Blood.x, self.Blood.y = 0, 0 self.Blood.rotation = math.random(360) self.Gleam = display.newImage(self, "effects/highlight.png") self.Gleam.x, self.Gleam.y = 0, 0 self:toBack()
add
mode: self.Gleam = display.newImage(self, "effects/highlight.png")
self.Gleam.x, self.Gleam.y = 0, 0
self.Gleam.blendmode = 'add'
self:toBack()
self.Gleam.blendMode = 'add'
self:setMask(clip)
self:toBack()
self.Blood.x, self.Blood.y = 0, 0 self.Blood.rotation = math.random(360) self.Gleam = display.newImage(self, "effects/highlight.png") self.Gleam.x, self.Gleam.y = 0, 0 self.Gleam.blendMode = 'add' self:setMask(clip) self.maskRotation = math.random(360) self:toBack()
self.Blood.rotation = math.random(360) local twist = math.random(-270, 270) if math.abs(twist) <= 135 then twist = twist * 2 end self.Gleam = display.newImage(self, "effects/highlight.png")
if math.abs(twist) <= 135 then
twist = twist * 2
end
transition.to(self.Blood, {time = 2000; delta = true; rotation = twist})
self.Gleam = display.newImage(self, "effects/highlight.png")
Notice that we don't need to change any of the other code that controls the effect, such as the collision code or the growing and shrinking. It all just works with the new object!
We assembled two layers into a single effect image, and applied a single mask to the whole assembly. We added animation selectively to some parts of the image but not others:
This ability to add a mask to an entire group is one of the most powerful aspects of Corona's masking feature. You can use it for everything from clipping different sections of a display, to creating special effects, to managing important game elements like a flashlight or torch radius effect when dungeon crawling.
3.137.185.180