Game Development Reference
In-Depth Information
function onAccelerometer(event)
-- capture the events here
end
Runtime:addEventListener("accelerometer", onAccelerometer)
Moving the Helicopter
Now that we've set up the accelerometer, we can move the helicopter based on the data returned
from the accelerometer. We set up an enterFrame event that runs 30 or 60 times per second
(depending on the fps setting and the responsiveness of your code).
Here's the code for Corona SDK:
if gameOver == true then return end
fWait = (wTime > 0)
if fWait then
wTime = wTime - 0.01
if wTime < 0 then
wTime = 0
end
return
end
if _hasAccel==true then
local gx, gy = getAcceleration()
fx = gx * filter + fx * (1-filter)
fy = gy * filter + fy * (1-filter)
updatePlayer(fx, fy)
end
end
We use the generic function getAcceleration() because Corona SDK returns the acceleration to the
function onAccelerometer . In Gideros, we need to poll the data when we need it.
Here's the code for Corona SDK:
local gpx, gpy, gpz
function onAccelerometer(event)
-- capture the events here
gpx, gpy, gpz = event.xGravity, event.yGravity, event.zGravity
end
 
Search WWH ::




Custom Search