Button Selection

Hi guys,

I’m having a hard time figuring out something that perhaps is simple but I thought I could reach out to you:

Let’s say I have a screen with 2 buttons that I select by swiping the apple remote. When the scene is loaded, I would like the button on the left to be selected, and then when the user swipes right, I’d like to select the button on the right. I get this working, but the thing is that before I can select the button on the right, when running the app for the first time I have to swipe twice. It seems like the app can’t detect the first swipe… I’m attaching the code below and if you could try it out just install the app on the apple tv and try swiping right… you will notice that it doesn;t work, unless you swipe again. Is that something I’m missing?

Cheers,

Marcos

local buttons = {} local selectedButton &nbsp; local button1 = display.newRect( &nbsp;0, 0, 100, 100 ) button1.x =100 button1.y = 100 &nbsp; local button2 = display.newRect( &nbsp;0, 0, 100, 100 ) button2.x =300 button2.y = 100 &nbsp; buttons[1] = button1 buttons[2] = button2 &nbsp; selectedButton = 1 buttons[selectedButton]:setFillColor( 0,1,1 ) &nbsp; &nbsp; local resetAt0 = true local function onAxisEvent( event ) &nbsp; local previousButton = selectedButton &nbsp; local mappedEvent = { name = "key", phase = "up", keyName = "" } local delta = event.normalizedValue &nbsp; if not event.axis or (event.axis.type ~= 'y' and event.axis.type ~= 'x') then return end &nbsp; if resetAt0 and delta ~= 0 then return end resetAt0 = false &nbsp; if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) resetAt0 = true end &nbsp; &nbsp; &nbsp; if mappedEvent.keyName ~= "" then Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) &nbsp; end end &nbsp; Runtime:addEventListener( "axis", onAxisEvent )

Anyone?

Perhaps try this?

... if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end resetAt0 = true end if mappedEvent.keyName ~= "" then buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) end ...

Thanks!!!

Anyone?

Perhaps try this?

... if math.abs(delta) \> 0.5 then if event.axis.type == "x" then if delta \< 0 then mappedEvent.keyName = "left" selectedButton = 1 elseif delta \> 0 then selectedButton = 2 mappedEvent.keyName = "right" end end resetAt0 = true end if mappedEvent.keyName ~= "" then buttons[previousButton]:setFillColor( 1 ) buttons[selectedButton]:setFillColor( 0,1,1 ) Runtime:dispatchEvent( mappedEvent ) print( "DISPATCHED: " .. mappedEvent.keyName ) end ...

Thanks!!!