Set accelerometer to user position

getting this error “game.lua Line: 319 Attempt to perform arithmetic on local ‘gy’ (a nil value)”

using this code : 

local gy, gz = event.yGravity, event.zGravity&nbsp; local len = math.sqrt(gy\*gy+gz\*gz) \* (gz \< 0 and -1 or 1) cos\_delta = gz / len sin\_delta = -gy / len &nbsp;

    

i am trying to set the accelerometers 0 point to the users position/the angel he is holding it 

Would have to see more of this code to help.  Can you show the entire function as well as where it gets called?

Can you post some more code?  Where is event coming from?  Where are you setting up the accelerometer handler?

Some more code : 

&nbsp; &nbsp; tiltSpeed &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 30; &nbsp;&nbsp;&nbsp;&nbsp;motionx &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; &nbsp;&nbsp;&nbsp;&nbsp;motiony &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; &nbsp;&nbsp;&nbsp;&nbsp;rotation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; --delta = -50/180\*math.pi &nbsp;-- 30 degrees, maybe should have minus sign --cos\_delta, sin\_delta = math.cos(delta), math.sin(delta) &nbsp;&nbsp;&nbsp;&nbsp; local gy, gz = event.yGravity, event.zGravity&nbsp; local len = math.sqrt(gy\*gy+gz\*gz) \* (gz \< 0 and -1 or 1) cos\_delta = gz / len sin\_delta = -gy / len &nbsp;&nbsp;&nbsp;&nbsp; local function onTilt(event) &nbsp; &nbsp; motionx = tiltSpeed \* event.xGravity &nbsp; &nbsp; motiony = tiltSpeed \* (cos\_delta\*event.yGravity + sin\_delta\*event.zGravity) end

event.yGravity, event.zGravity  are not in the event function so event.yGravity, event.zGravity  = nil so gy,gz = nil

This line of code:

local gy, gz = event.yGravity, event.zGravity

 

is executed outside of your onTilt function.  At that point your app doesn’t know what event is and it’s certainly not populating the xGravity, yGravity and zGravity values.   You have to do these calculations inside your onTilt() function.  You will have to have a flag indicating that you need to save the first values and the first time onTilt() is called, then you can set your baseline.

Would have to see more of this code to help.  Can you show the entire function as well as where it gets called?

Can you post some more code?  Where is event coming from?  Where are you setting up the accelerometer handler?

Some more code : 

&nbsp; &nbsp; tiltSpeed &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 30; &nbsp;&nbsp;&nbsp;&nbsp;motionx &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; &nbsp;&nbsp;&nbsp;&nbsp;motiony &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; &nbsp;&nbsp;&nbsp;&nbsp;rotation &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 0; --delta = -50/180\*math.pi &nbsp;-- 30 degrees, maybe should have minus sign --cos\_delta, sin\_delta = math.cos(delta), math.sin(delta) &nbsp;&nbsp;&nbsp;&nbsp; local gy, gz = event.yGravity, event.zGravity&nbsp; local len = math.sqrt(gy\*gy+gz\*gz) \* (gz \< 0 and -1 or 1) cos\_delta = gz / len sin\_delta = -gy / len &nbsp;&nbsp;&nbsp;&nbsp; local function onTilt(event) &nbsp; &nbsp; motionx = tiltSpeed \* event.xGravity &nbsp; &nbsp; motiony = tiltSpeed \* (cos\_delta\*event.yGravity + sin\_delta\*event.zGravity) end

event.yGravity, event.zGravity  are not in the event function so event.yGravity, event.zGravity  = nil so gy,gz = nil

This line of code:

local gy, gz = event.yGravity, event.zGravity

 

is executed outside of your onTilt function.  At that point your app doesn’t know what event is and it’s certainly not populating the xGravity, yGravity and zGravity values.   You have to do these calculations inside your onTilt() function.  You will have to have a flag indicating that you need to save the first values and the first time onTilt() is called, then you can set your baseline.