How do I use compass 'heading' event?
I added an 'heading' runtime event listener to a very simple test app so I could see the compass values but the event never initiates. How does a heading event execute?
I think you need to convert event.magnetic to a string first.
local mag = string.format( '%.0f', event.magnetic )
msg:setText( mag .. "°" )
Try that.
Carlos
I'm still at my _real_ job so no access to my corona code, but now that you mention it I recall I did do an even simpler test just to see if the event calls my function, which it didn't, like so:
local msg = display.newText( "Compass Heading", 50, 50, nil, 24 )
msg:setTextColor( 255,255,255 )
msg.text = "hello"
local function showHeading( event )
msg.text = "there"
-- msg.text = event.magnetic
end
Runtime:addEventListener( "heading", showHeading)
the message displayed was "hello" and didn't change. I will certainly try your suggestion when I get home tonight. thanks for the reply.
Jim
Hi, Jimbo. I just pasted in and built your first example code (from your 14:37 post), and it worked fine on the phone! Are you sure you built for iPhone OS 3.0 rather than 2.2.1?
Also, the compass events only occur on the 3GS (not in the simulator), but I assume that's not the issue...
I thought I did 3.0 build, but it's great to hear it worked for you. I must have done something wrong. Soon as I get home I'll redo the build and confirm 3.0. yes, I've got 3Gs and did run in phone, not simulator.
Many thanks for all the quick responses!
yup. my build was 2.2.1. interestingly the 'location' event works (which I forgot to mention) in that build.
thanks again
Glad it works.
Carlos


I don't have the code in front of me but it was something very simple like:
local msg = display.newText( "Compass Heading", 50, 50, nil, 24 )
msg:setTextColor( 255,255,255 )
local function showHeading( event )
msg.text = event.magnetic
end
Runtime:addEventListener( "heading", showHeading)
and the value never displayed