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 – adding the egg object


Imagine a world, full of falling eggs. It's not entirely too realistic, but in this game, we're creating this element. At least, we'll be making sure that both gravity and real-world physics will be applied. To add the egg object, perform the following steps:

  1. Create a new local function called eggDrop():

        local eggDrop = function()
  2. Add in the egg display object properties:

          local egg = display.newImageRect( "egg.png", 26, 30 )
          egg.x = 240 + mRand( 120 ); egg.y = -100
          egg.isHit = false
          physics.addBody( egg, "dynamic",{ density=eggDensity, bounce=0, friction=0.5, shape=eggShape } )
          egg.isFixedRotation = true
          gameGroup:insert( egg )
  3. Add in the postCollision event for the egg display object:

          egg.postCollision = onEggCollision
          egg:addEventListener( "postCollision", egg )
        end

What just happened?

We have set the egg value for x with 240 + mRand( 120 ). The mRand function is equal to math.random, which will allow...