Time for action – creating the egg collision

We have handled collisions in the previous sample games we created. Handling postcollisions requires the introduction of force to execute the completion of a postcollision event:

  1. Create a new local function called onEggCollision() with two parameters called self and event:
        local onEggCollision = function( self, event )
  2. Create an if statement when the force is greater than 1 and include not self.isHit. Add in the eggCaughtSound sound effect:
          if event.force > 1 and not self.isHit then
            audio.play( eggCaughtSound )
  3. Make self invisible and inactive, and replace it with the friedEgg display object:
            self.isHit = true
            print( "Egg destroyed!")
            self.isVisible = false
            friedEgg.x = self.x; friedEgg.y = self.y
            friedEgg.alpha = 0
            friedEgg.isVisible = true
  4. Create a function that transitions the friedEgg display object and fades it off the stage by using the onComplete command:
            local fadeEgg = function()
              transition.to( friedEgg, { time=500, alpha=0 } )
            end
            transition.to( friedEgg, { time=50, alpha=1.0, onComplete=fadeEgg } )
            self.parent:remove( self )
            self = nil
  5. Using if event.other.myName == "character", update eggCount when the main character catches the eggs. Also, update gameScore by 500 points for every collision. If the egg hits the ground, use elseif event.other.myName == "ground" and decrement the lives using livesCount():
            if event.other.myName == "character" then
              eggCount = eggCount + 1
              eggText.text = "Caught: " .. eggCount
              eggText.xScale = 0.5; eggText.yScale = 0.5  --> for clear retina display text
              eggText.x = (480 - (eggText.contentWidth * 0.5)) - 15
              eggText.y = 305
              print("egg caught")
              local newScore = gameScore + 500
              setScore( newScore )
            elseif event.other.myName == "ground" then
              livesCount()
              print("ground hit")
            end
          end
        end
    Time for action – creating the egg collision

Using onEggCollision( self, event ), we set up the function with an if statement for event.force > 1 and not self.isHit. When both statements return true, the sound effect for the egg plays. The initial egg falling from the sky is removed from the scene upon collision and replaced by the friedEgg display object in the same location, using friedEgg.x = self.x; friedEgg.y = self.y.

The fadeEgg()function makes the newly replaced egg object appear in 50 milliseconds by transition.to( eggCrack, { time=50, alpha=1.0, onComplete=fadeCrack } ) and then with the onComplete command, it returns the object to an invisible state with transition.to( eggCrack, { time=500, alpha=0 } ).

When the name "character" is called from event.other.myName, every collision assigned to that name increments eggCount + 1. Therefore, eggText is updated with the eggCount value. The setScore( newScore ) statement increments the score by 500 with every collision made to "character". When a collision is made to "ground", the livesCount() function is called, which subtracts life by 1.

We will apply the main asset (egg object) by learning how to add physical objects to the scene and have them fall in random areas in the game. The physics engine will take into account a dynamic physics body that we will create for the egg display object.