Applying the dissolve

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.

Getting ready

Open the file creep.lua and locate the gap just above the body of the module function, after the table of sprite sequences.

Getting on with it

The first thing that needs to be done is to load the mask file so that it can be used on the zombie sprites.

  1. One mask object can be shared among any number of objects, so we'll load it into a variable at the top level of the file:
        start = 12, count = 1;
      },
    }
    
    local dissolve = graphics.newMask("effects/dissolve.png")
    
    return function(goal, x, y)
  2. While we're here, we'll add a constant declaration for the distance the mask will travel to move it completely across the sprite until it's invisible. As we discussed in the Planning the dissolve section, this distance will be negative since it's moving away from the direction it's turned towards:
    local dissolve = graphics.newMask("effects/dissolve.png")
    local dissolveDisplacement = -72
    
    return function(goal, x, y)
  3. The mask is now ready and available to the function that needs it; the 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
  4. The mask's grain (the size and spacing of the black areas) is a little coarse for these fairly small images, so we'll scale it down to make it seem finer:
        self:setMask(dissolve)
        self.maskScaleX, self.maskScaleY = 0.5, 0.5
        clear(self)

    Note

    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.

  5. Next, determine the angle between the dying zombie and the object that triggered its death and was passed into the 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)
  6. Turn the mask to face this direction. Remember that Lua's trigonometric functions return results in radians, but Corona's rotation properties expect values in degrees.
        local theta = math.atan2(cause.y - self.y, cause.x - self.x)
        self.maskRotation = math.deg(theta) % 360
        clear(self)
  7. Next, animate the mask's position to move a negative amount in that direction. The transition library will animate any numerical property of value of the specified object, so it can control 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)
  8. Finally, instead of clearing the object instantly on death, we want to hold off until the animation is over:
        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!

What did we do?

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.

What else do I need to know?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.39.23