Time for action – creating the panda character

We need to set up the panda collision event and animate it accordingly, using the image sheet:

  1. We need to create a local function that will introduce the collision and touch events for the panda. We shall call it createPanda():
    local createPanda = function()
  2. When the panda collides with the stars, use onPandaCollision() with the parameters self and event. Reload panda every time a collision occurs with the stars or the edge of the screen, by using callNewRound():
      local onPandaCollision = function( self, event )
        if event.phase == "began" then
    
          if panda.isHit == false then
    
            panda.isHit = true
    
            if event.other.myName == "star" then
              callNewRound( true, "yes" )
            else
              callNewRound( true, "no" )
            end
    
            if event.other.myName == "wall" then
              callNewRound( true, "yes" )
            else
              callNewRound( true, "no" )
            end
    
            elseif panda.isHit then
              return true
            end
        end
      end
  3. Create a directional arrow to allow the user to aim for an area to launch the panda. Insert it to the gameGroup group:
      arrow = display.newImage( "arrow.png" )
      arrow.x = 240; arrow.y = 225
      arrow.isVisible = false
    
      gameGroup:insert( arrow )
  4. Create the image sheet for the panda that has three different animation sequences called "set", "crouch", and "air":
      local sheetData = { width=128, height=128, numFrames=5, sheetContentWidth=384, sheetContentHeight=256 }
      local sheet = graphics.newImageSheet( "pandaSprite.png", sheetData )
    
      local sequenceData = 
      {
        { name="set", start=1, count=2, time=200 }, 
        { name="crouch", start=3, count= 1, time=1 }, 
        { name="air", start=4, count=2, time=100 }  
      }
    
      panda = display.newSprite( sheet, sequenceData )
    
      panda:setSequence("set")
      panda:play()
  5. Add the following properties to panda before it launches into the air:
      panda.x = 240; panda.y = 225
      panda.isVisible = false
    
      panda.isReady = false
      panda.inAir = false
      panda.isHit = false
      panda.isBullet = true
      panda.trailNum = 0
    
      panda.radius = 12
      physics.addBody( panda, "static", { density=1.0, bounce=0.4, friction=0.15, radius=panda.radius } )
      panda.rotation = 0
  6. Set up collisions for panda using "collision" and apply an event listener:
      panda.collision = onPandaCollision
      panda:addEventListener( "collision", panda )
  7. Create the poof object:
      poof = display.newImage( "poof.png" )
      poof.alpha = 1.0
      poof.isVisible = false
  8. Insert the panda and poof objects into the gameGroup group. Close the function:
      gameGroup:insert( panda )
      gameGroup:insert( poof )
    end
  9. We'll need to scroll up to the activateRound() function and add the "set" animation sequence for the panda:
      panda:setSequence("set")
      panda:play()

The collision events that occur for the panda start with if event.phase == "began". The panda reloads on screen through several cases of if statements. event.other.myName == "star" will call a new round when the panda launches off screen towards the right, left, or top sides of the stage.

The image sheet for the panda has three sets of animations. They are called "set", "air", and "crouch". There are a total of five frames in the image sheet.

The physical properties of the panda are set before launch. The body type is set to "static" and will change when it's in the air.

The collision event for the panda is called by panda:addEventListener( "collision", panda ).

Now that the image sheet has been set up, the "set" animation needs to be added in the activateRound() function to initiate movement.