Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7. Physics: Falling Objects > Time for action – making the egg drop

Time for action – making the egg drop

We're going to execute the timer for the egg so they can start dropping them on the screen.

  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, startDrop. Add the statement if gameLives < 1 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 = timer.performWithDelay( 1000, eggDrop, 0 ).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial