Login / Account Info

How do I use compass 'heading' event?

7 replies [Last post]
Jimbo
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 16 Dec 2009

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?

Jimbo
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 16 Dec 2009

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

carlos
User is online Online


Ansca Staff

Joined: 22 Jun 2009

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

Jimbo
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 16 Dec 2009

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

evank
User offline. Last seen 2 days 9 hours ago. Offline


Ansca Staff

Joined: 16 Sep 2009

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...

Jimbo
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 16 Dec 2009

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!

Jimbo
User offline. Last seen 4 weeks 3 days ago. Offline
Joined: 16 Dec 2009

yup. my build was 2.2.1. interestingly the 'location' event works (which I forgot to mention) in that build.
thanks again

carlos
User is online Online


Ansca Staff

Joined: 22 Jun 2009

Glad it works.

Carlos

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.