Runtime listener calling other runtime listener?

Hi,

we’re working on an app that measures the angle of the iPhone and retrieves the compass location.
However, I’m having some issues processing the events.

What I would like is to fire the listeners, and kill them after a short timeout, storing the last recorded value. and only after these both succeeded display the results to the user.

However, I tried the following:

local MeasureHeading  
local mAngle, gotAngle  
local userHeading, gotHeading  
  
local function MeasureAngle ( event )   
 local yangle = 0   
 mAngle = math.abs(90 \* event.yGravity)  
 gotAngle = true  
 if gotAngle and (mAngle ~= 0) then  
 Runtime:removeEventListener( "accelerometer", MeasureAngle )  
 MeasureResults.text = "Angle: " .. mAngle   
 timer.performWithDelay(500, Runtime:addEventlistener( "heading", MeasureHeading))  
 end  
end  
  
function MeasureHeading ( event )  
 local geoheading, magheading  
 magheading = math.round(event.magnetic)  
 if system.getInfo( "platformName" ) ~= "Android" then  
 geoheading = math.round(event.geographic)  
 else   
 geoheading = magheading  
 end  
  
 gotHeading = true  
 if gotHeading then  
 Runtime:removeEventListener( "heading", MeasureHeading )  
 userHeading = geoheading  
 MeasureResults.text = MeasureResults.text .. " Orientation: " .. userHeading  
 resultsScreen:insert(MeasureResults)  
 end  
end  
  
local function measurementScreen ()  
 -- here is where the main stuff goes  
 Runtime:addEventListener("accelerometer", MeasureAngle)  
 -- meanwhile some other processing takes place  
end  
  

now what I get in the debugger output once the MeasureAngle is triggered and was done measuring is:

[code]…/CoronaDev/TestApp/automatic_measurement.lua:186: attempt to call method ‘addEventlistener’ (a nil value)

or
Runtime error
…/CoronaDev/TestApp/automatic_measurement.lua:179: attempt to call method ‘addEventlistener’ (a nil value)
stack traceback:
[/code]

Sp the above triggers a runtime error.
I tried starting both the eventlisteners in the measurementScreen function, this did not return an error, but I am lost where to check that both have succeeded (in this case, MeasureAngle does not try to call MeasureHeading of course):

local function measurementScreen ()  
 -- here is where the main stuff goes -- this is triggered by a "tap" on the start button  
 Runtime:addEventListener("accelerometer", MeasureAngle)  
 Runtime:addEventListener("heading", MeasureHeading)  
 -- meanwhile some other processing takes place  
 -- if I put the check here (e.g. if gotHeading and gotAngle then..)  
 -- this will be performed before the above is finished, and the results are incorrect (usually showing the previous result)  
 -- kill the listeners again.  
  
end  

Maybe this is a n00b problem, I’m not doing this for a long time, but after trying anything I could come up with I am still lost.

In short, what I want the above to do is:

  • accelerometer and heading measurement start
  • once they have an angle after e.g. 1 second, store that and
  • only then display the result.

Or is the only way to do this to add another “tap” event to the “start” button to stop the measurement and store the result and display it?
Or should I start these listeners at the start of the app, in the main.lua and leave them running until finished?

thanks! [import]uid: 106658 topic_id: 24416 reply_id: 324416[/import]