Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Button Selection
Started by Marcos Martini Apr 02 2016 10:38 PM

3 replies to this topic
tvos appletv axis button
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Marcos Martini

[GLOBAL: userInfoPane.html]
Marcos Martini
  • Enthusiast

  • 37 posts
  • Corona SDK

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
 
local button1 = display.newRect(  0, 0, 100, 100 )
button1.x =100
button1.y = 100
 
local button2 = display.newRect(  0, 0, 100, 100 )
button2.x =300
button2.y = 100
 
buttons[1] = button1
buttons[2] = button2
 
selectedButton = 1
buttons[selectedButton]:setFillColor( 0,1,1 )
 
 
local resetAt0 = true
local function onAxisEvent( event )
 
local previousButton = selectedButton
 
local mappedEvent = { name = "key", phase = "up", keyName = "" }
local delta = event.normalizedValue
 
if not event.axis or (event.axis.type ~= 'y' and event.axis.type ~= 'x') then
return
end
 
if resetAt0 and delta ~= 0 then
return
end
resetAt0 = false
 
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
 
    if mappedEvent.keyName ~= "" then
Runtime:dispatchEvent( mappedEvent )
print( "DISPATCHED: " .. mappedEvent.keyName )
 
end
end
 
Runtime:addEventListener( "axis", onAxisEvent )


[TOPIC: post.html]
#2

Marcos Martini

[GLOBAL: userInfoPane.html]
Marcos Martini
  • Enthusiast

  • 37 posts
  • Corona SDK

[TOPIC: post.html]
#3

JonPM

[GLOBAL: userInfoPane.html]
JonPM
  • Corona Geek

  • 1,077 posts
  • Corona SDK

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

...


[TOPIC: post.html]
#4

Marcos Martini

[GLOBAL: userInfoPane.html]
Marcos Martini
  • Enthusiast

  • 37 posts
  • Corona SDK

Thanks!!!




[topic_controls]
[/topic_controls]