Time for action – creating star collisions

Star collisions need to be made and removed from the stage so that points can be accumulated for the player.

  1. Create a function for the star collision called onStarCollision() and have a self and event parameter:
    local onStarCollision = function( self, event )
  2. Add the if statements that remove the stars children from the game screen when a collision is made. Increment the score by 500 for each star removed from the screen. Close the function with end:
      if event.phase == "began" and self.isHit == false then
    
        self.isHit = true
        print( "star destroyed!")
        self.isVisible = false
    
        stars.numChildren = stars.numChildren - 1
    
        if stars.numChildren < 0 then
          stars.numChildren = 0
        end
    
        self.parent:remove( self )
        self = nil
    
        local newScore = gameScore + 500
        setScore( newScore )
      end
    end
    Time for action – creating star collisions

The star collision occurs on first contact with if event.phase == "began" and self.isHit == false, assuming the star has not been touched by the panda. The stars are removed from the screen by self.parent:remove( self ) and self = nil. The score is incremented by 500 through gameScore and updated to setScore = (scoreNum).

Try tracking how many stars the panda catches during game play. The logic is similar to how the game score was created. Each star that is caught will have to increment by 1 as the count for every collision made. The star count is placed within the onStarCollision() function. A new function and method will have to be created to display the text of the star count, and will have to be updated every time the count changes.