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 – moving the character


Eggs will be falling in all different areas of the screen from the sky. Let's prepare our main character to move through all the potential areas on the screen:

  1. Set up a new local function called moveChar() with an event parameter:

    local moveChar = function(event)
  2. Add in the accelerometer movement for the character:

      charObject.x = display.contentCenterX - (display.contentCenterX* (event.yGravity*3))
  3. Create character boundaries where it moves on the screen. This enables the character to stay within the game screen and not go past the offscreen boundaries:

      if((charObject.x - charObject.width * 0.5) < 0) then charObject.x = charObject.width * 0.5
      elseif((charObject.x + charObject.width * 0.5) > display.contentWidth) then
      charObject.x = display.contentWidth - charObject.width * 0.5
      end
    end

What just happened?

To make the accelerometer movement work with a device, we have to use yGravity.

Note

Accelerometer events are based on portrait scale when...