Assembling the splatter layers

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.

Getting ready

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.

Getting on with it

Start by opening the file blast.lua and perform the following steps:

  1. Near the beginning of the file, load the mask:
    local category = require "category"
    
    local clip = graphics.newMask("effects/splat.png")
    
    local function shrink(object)
  2. Next, locate the first lines of the main function where a circle is created to represent the splatter and set to appear as red. Change these functions to create a new group parented to the specified world, positioned at a specified point:
    return function (world, cause, x, y)
      local self = display.newGroup()
      world:insert(self)
      self.x, self.y = x, y
    
      self:toBack()
  3. Like the mask image in the zombie example, the blood image isn't currently set to the same size we were using before, so we'll adjust the scale:
      world:insert(self)
      self.x, self.y = x, y
      self:scale(25/32, 25/32)
    
      self:toBack()
  4. Next, we'll load the blood texture and center it over the group's origin. This keeps the positioning consistent with that of a single image:
      self:scale(25/32, 25/32)
      self.Blood = display.newImage(self, "effects/blood.png")
      self.Blood.x, self.Blood.y = 0, 0
    
      self:toBack()
  5. We'll load the highlight image into the same group, and make sure it's positioned like the blood texture:
      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()
  6. We'll set the highlight to add mode:
      self.Gleam = display.newImage(self, "effects/highlight.png")
      self.Gleam.x, self.Gleam.y = 0, 0
      self.Gleam.blendmode = 'add'
    
      self:toBack()
  7. Next, we'll attach the mask. Like the zombie dissolve mask, a single loaded mask can be used for all these objects:
      self.Gleam.blendMode = 'add'
      self:setMask(clip)
    
      self:toBack()
  8. We're almost done. To create that variation we discussed in the previous section, rotate the mask and the texture to random positions:
      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()
  9. Finally, we'll animate the blood texture. We'll give it a random turning speed in either direction; the doubling code makes sure that it has a minimum intensity regardless of the direction it's turning in.
      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")
  10. To finish the animation code, we just need to actually start the animation:
      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!

What did we do?

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:

What did we do?

What else do I need to know?

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.

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

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