Now that we have the right assets and a plan to use them for the desired effect, it's time to write the actual code.
Open the file creep.lua
and locate the gap just above the body of the module function, after the table of sprite sequences.
The first thing that needs to be done is to load the mask file so that it can be used on the zombie sprites.
start = 12, count = 1;
},
}
local dissolve = graphics.newMask("effects/dissolve.png")
return function(goal, x, y)
local dissolve = graphics.newMask("effects/dissolve.png")
local dissolveDisplacement = -72
return function(goal, x, y)
self:Die
function inside the constructor, which causes a creep that's touched a hazard to remove itself, leave a pool of hazard, and notify the world. First we'll add the mask to the object that's about to be destroyed: function self:Die(cause)
blast(world, cause, self.x, self.y)
world:dispatchEvent{name='Death'; unit = self, source = cause, worth = 10, chain = cause.Chain}
world:removeEventListener('clock', self)
self:setMask(dissolve)
clear(self)
end
self:setMask(dissolve)
self.maskScaleX, self.maskScaleY = 0.5, 0.5
clear(self)
For a commercial project, we'd probably want to scale the mask image itself down before release, to produce a cleaner result, but often when a project is in early development, it's more useful to get it running quickly with the assets already at hand, to see results quickly and make changes easily.
Die
function as the cause argument: self:setMask(dissolve)
self.maskScaleX, self.maskScaleY = 0.5, 0.5
local theta = math.atan2(cause.y - self.y, cause.x - self.x)
clear(self)
local theta = math.atan2(cause.y - self.y, cause.x - self.x)
self.maskRotation = math.deg(theta) % 360
clear(self)
maskX
and maskY
just as easily as it can the object's own position or scale.self.maskRotation = math.deg(theta) % 360 transition.to(self, { maskX = dissolveDisplacement * math.cos(theta), maskY = dissolveDisplacement * math.sin(theta); ) display.clear(self)
transition.to(self, { maskX = dissolveDisplacement * math.cos(theta), maskY = dissolveDisplacement * math.sin(theta); onComplete = display.clear} ) end
You can now test the project on either the simulator or a device and watch the zombies evaporate when a splat touches them!
We created a reusable mask that can be attached to each monster object as it dies, and pointed it in the correct direction to appear to come from the direction of whatever killed the creature. We used the transition library to easily manage the movement of the mask that creates the impression of disappearing.
The display.clear
function isn't a standard part of the Corona display
module. It's effectively a version of display.remove
that includes sanity checks as to whether an object is still usable, mostly in cases where the object has been cleared from the scene before its transition finishes. Since it will be used to remove both creeps and blasts, it's in a module of its own, similar to the math.pythagorean
function
in earlier projects.
Of course, you can also animate the other properties of an object that govern its mask, such as its rotation and scale.
13.58.39.23