Time for action – reloading the panda on the stage

When the panda has been in the air for a certain amount of time or has hit any out-of-bounds areas off the screen, it will turn into a cloud of smoke. The panda will be replaced with a "poof" image when a collision event occurs with the edge of the screen or the ground. The visible properties of the panda have to be turned off for the "poof" effect to work. When the collision has been made, the panda needs to be reloaded back onto the screen while the game is still activated.

  1. Create a local function called callNewRound(). Include a local variable called isGameOver and set it to false:
    local callNewRound = function()
      local isGameOver = false
  2. Within the current function, create a new local function called pandaGone(). Add in the new properties for the panda, so it no longer displays on the game stage:
      local pandaGone = function()
    
        panda:setLinearVelocity( 0, 0 )
        panda.bodyType = "static"
        panda.isVisible = false
        panda.rotation = 0
    
        poof.x = panda.x; poof.y = panda.y
        poof.alpha = 0
        poof.isVisible = true
  3. Add in a new function for the poof object called fadePoof(). With the onComplete command, transition with time set to 50 and alpha set to 1. Have the poof object fade out with time set to 100 and alpha set to 0. Close the pandaGone() function and call out to it using timer.performWithDelay:
        local fadePoof = function()
           transition.to( poof, { time=100, alpha=0 } )
        end
        transition.to( poof, { time=50, alpha=1.0, onComplete=fadePoof } )
    
        restartTimer = timer.performWithDelay( 300, function()
           waitingForNewRound = true; 
           end, 1)
    
      end
    
      local poofTimer = timer.performWithDelay( 500, pandaGone, 1 )
  4. When isGameOver is still false, add in a timer.performWithDelay method for startNewRound(). Close the callNewRound() function:
      if isGameOver == false then
        restartTimer = timer.performWithDelay(1500, startNewRound, 1)
      end
    end

A new round is called when the panda is no longer displayed on the screen and the clock is still counting down. When isGameOver is still false, then the panda reloads by calling startNewRound().

The panda collision occurs through pandaGone(). All physical properties become inactive by applying panda.isVisible = false.

The smoke appears exactly where the panda disappeared. This happens when poof.x = panda.x; poof.y = panda.y. poof becomes visible for a short while through fadePoof(). Once it has faded, a new round awaits, which sets waitingForNewRound to true.