Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By : Michelle M Fernandez
Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By: Michelle M Fernandez

Overview of this book

Table of Contents (19 chapters)
Corona SDK Mobile Game Development Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – making the egg drop


We're going to execute the timer for the eggs, so that they can start dropping on the screen. To make the egg drop, perform the following steps:

  1. Create a local function called eggTimer() and use timer.performWithDelay to drop an egg every 1 second (1000 milliseconds) repeatedly. Use eggDrop() to activate the drop:

        local eggTimer = function()
          startDrop = timer.performWithDelay( 1000, eggDrop, 0 )
        end
  2. Within the first if statement in the onEggCollision() function, cancel the timer using the timerID and startDrop variables. Add the if gameLives < 1 statement then to stop the eggs from falling:

          if gameLives < 1 then
            timer.cancel( startDrop )
            print("timer cancelled")
          end

What just happened?

In order for the eggs to start dropping from the sky, we created a function called eggTimer(). It activates the eggDrop() function by letting an egg drop after 1000 milliseconds (1 second) every time infinitely using startDrop...