Tilt Monster - Switching from Accelerometer to Touch based control

Written by Paul Allan Harrington

For a variety of products we are working on at VisualsWork.com we plan on incorporating many of the features and game play of Jonathan and Biffy Beebe's (http://beebegames.kodingen.com/) open sourced Tilt Monster app ( http://developer.anscamobile.com/code/tilt-monster). For much of our intended audience, our first course of action was to switch out the "Accelerometer" based player control for one that was "Touch" based.

For our initial testing, this is what we did:

Remarked out Runtime:addEventListener( "touch", touchPause ) on Line: 1185 in maingame.lua which also removes the "pause" feature as we just removed (remarked out) the "Event Listener" that was calling the "touchPause" function each time the screen was touched. We will likely want to create a button for the pause feature in future revisions and I'll share the code when we do. To remark out the code simply insert -- in before the appropriate code.

Example: --Runtime:addEventListener( "touch", touchPause )

For moving the player left or right depending on which side of the screen the user touched, we did the following:

Add a Runtime Event Listener to call a function we will create called "touchMove".

Example: Runtime:addEventListener( "touch", touchMove )

Add a local variable named "playerDirection" and function called "touchMove" for controlling the player direction:

-- Added to Switch from Accelerometer to Touch based control
local playerDirection -- used to control left and right player movement when playerObject.x = playerObject.x + playerDirection is added to the gameLoop function

local touchMove = function( event)

     -- Check to see if the screen is being "touched"
     if (event.phase == "began") then

          -- If the left side of the screen is being "touched" decrease playerDirection (makes the player move left)
          if event.x < display.contentWidth / 2 then

               playerDirection = -4

          end

          -- If the right side of the screen is being "touched" increase the playerDirection (makes the player move right)
          if event.x > display.contentWidth / 2 then

               playerDirection = 4

          end

     end

          -- Returns the playerDirection value to 0 (straight) when the screen is no longer being "touched"
          if (event.phase == "ended") then

               playerDirection = 0

          end

     return true

     end

Finally, I added the following line to the gameLoop function just above the call to moveTrees() around Line: 4242 :

playerObject.x = playerObject.x + playerDirection

Give it a try and feel free to contact me if you have questions, comments and suggestions.

*Update: When testing on iOS devices (not in the Corona Simulator), we learned that we forgot to disable the Accelerometer control when implementing our "Touch" based control. Initial testing also hinted at the need to "rethink" how we can provide a solution that isn't compromised when a user touches both the left and right side of the screen simultaneously. Stay tuned as we explore and post future revisons.