Time for action – starting the game

Right now, we need to set the offscreen position for the panda and have it transition to its starting launch location, so the user can interact with it.

  1. After adding the variables, create a new local function called startNewRound() and add an if statement to initiate the panda object into the scene:
    local startNewRound = function()
      if panda then
  2. Add a new local function called activateRound() within startNewRound(). Set the starting position of the panda display object on screen and add ground:toFront(), so that the ground appears in front of the panda character:
      local activateRound = function()
    
        waitingForNewRound = false
    
        if restartTimer then
          timer.cancel( restartTimer )
        end
    
        ground:toFront()
        panda.x = 240
        panda.y = 300
        panda.rotation = 0
        panda.isVisible = true
  3. Create another local function called pandaLoaded(). Set gameIsActive to true and set the panda object's air and hit properties to false. Add panda:toFront() so that it appears in front of all the other game objects on screen and set the body type to "static":
        local pandaLoaded = function()
    
          gameIsActive = true
          panda.inAir = false
          panda.isHit = false
          panda:toFront()
    
          panda.bodyType = "static"
    
        end
  4. Transition the panda to y=225 in 1,000 milliseconds. When the tween is completed, call the pandaLoaded() function using the onComplete command. Close the activateRound() function with end and call out to it. Close the if statement for panda and the startNewRound() function with end:
        transition.to( panda, { time=1000, y=225, onComplete=pandaLoaded } )
        end
    
        activateRound()
    
      end
    end
    Time for action – starting the game

When the level is activated, the panda is placed below the ground before it is visible to the player. For pandaLoaded(), the game is activated by gameIsActive = true, and the panda is ready for launch by the player. The panda transitions from the ground level to an area on the screen where it can be accessed.