Time for action – launching the panda

Let's add a touch event for the panda so that it flings toward the stars. The powerShot object will play a role in helping the player visualize how much power needs to be applied to the panda, before it launches into the air.

  1. Implement touch events for the panda. Create a local function called onScreenTouch() with an event parameter:
    local onScreenTouch = function( event )
  2. With gameIsActive initiated, add in an if statement for when the touch event starts, by using event.phase == "began". During this event, use the "crouch" animation set to prepare panda for launch:
      if gameIsActive then
        if event.phase == "began" and panda.inAir == false then
    
          panda.y = 225
          panda.isReady = true
          powerShot.isVisible = true
          powerShot.alpha = 0.75
          powerShot.x = panda.x; powerShot.y = panda.y
          powerShot.xScale = 0.1; powerShot.yScale = 0.1
    
          arrow.isVisible = true
    
          panda:setSequence("crouch")
          panda:play()
  3. Add an elseif statement for when the touch event ends by using event.phase == "ended". Create a new local function called fling(), which will hold the properties of panda when it is launched toward the star objects. Apply a force opposite to where the touch event is dragged. Scale the powerShot display object outward when the touch event is pulled farther from the character:
        elseif event.phase == "ended" and panda.isReady then
    
          local fling = function()
            powerShot.isVisible = false
            arrow.isVisible = false
    
            local x = event.x
            local y = event.y
            local xForce = (panda.x-x) * 4
            local yForce = (panda.y-y) * 4
    
            panda:setSequence("air")
            panda:play()
    
            panda.bodyType = "dynamic"
            panda:applyForce( xForce, yForce, panda.x, panda.y )
            panda.isReady = false
            panda.inAir = true
    
          end
    
        transition.to( powerShot, { time=175, xScale=0.1, yScale=0.1, onComplete=fling} )
    
        end
    
        if powerShot.isVisible == true then
    
          local xOffset = panda.x
          local yOffset = panda.y
    
          local distanceBetween = mCeil(mSqrt( ((event.y - yOffset) ^ 2) + ((event.x - xOffset) ^ 2) ))
    
          powerShot.xScale = -distanceBetween * 0.02
          powerShot.yScale = -distanceBetween * 0.02
    
          local angleBetween = mCeil(mAtan2( (event.y - yOffset), (event.x - xOffset) ) * 180 / mPi) + 90
    
          panda.rotation = angleBetween + 180
          arrow.rotation = panda.rotation
        end
    
      end
    end
    Time for action – launching the panda

Once the game is active and the panda has been loaded on the screen, a touch event to launch the panda can be initiated. The panda will go from a "static" physics state to a "dynamic" physics state. The powerShot display object size increases the farther back the panda is pulled by an event touch.

The force from the panda launch is applied by local fling = function(). Launch force is created by xForce and yForce. The panda object is propelled by panda:applyForce( xForce, yForce, panda.x, panda.y ). Notice that the body type changes to "dynamic", so gravity can affect the object.