Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By : Michelle M Fernandez
Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By: Michelle M Fernandez

Overview of this book

Table of Contents (19 chapters)
Corona SDK Mobile Game Development Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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

What just happened?

The star collision occurs on first contact with if event.phase == "began" and self.isHit == false, assuming the...