widget.newSwitch not working with Graphics 2.0

I have an issue with an App I have been working on.

I have a row of switches which enable people to choose various items. Problem is only the first switch works.

This is the code to place the switches into the group.

In this instance the ‘Small’ switch will change states, but the ‘Regular’ and ‘Large’ switches do not change states or even register a press.

I recently updated to Graphics 2.0 and previous to the upgrade all the switches worked correctly.

I hope someone can help me with this problem. I am sure I am missing something really obvious.

-- create size - SmallsizeSmallText = display.newEmbossedText( "Small", 0, 0, myData.menuFont, 20 ) sizeSmallText.anchorX, sizeSmallText.anchorY = 0, .5 sizeSmallText:setFillColor( 255/255 ) sizeSmallText.x = sizeText.x + 55 sizeSmallText.y = sizeText.y + 2 sizeSmallText.alpha = 0 widgetGroup:insert(sizeSmallText) checkBoxSmall = widget.newSwitch { left = 100, top = sizeText.y - 20, style = "checkbox", id = "Small", initialSwitchState = false, onPress = onSizePress, } checkBoxSmall.alpha = 0 widgetGroup:insert(checkBoxSmall) -- create size - regular sizeRegularText = display.newEmbossedText( "Regular", 0, 0, myData.menuFont, 20 ) sizeRegularText.anchorX, sizeRegularText.anchorY = 0, 0.5 sizeRegularText:setFillColor( 255/255 ) sizeRegularText.x = sizeText.x + 135 sizeRegularText.y = sizeText.y + 2 sizeRegularText.alpha = 0 widgetGroup:insert(sizeRegularText) checkBoxRegular = widget.newSwitch { left = 190, top = sizeText.y - 20, style = "checkbox", id = "Regular", initialSwitchState = false, onPress = onSizePress, } checkBoxRegular.alpha = 0 widgetGroup:insert(checkBoxRegular) -- create size - Large sizeLargeText = display.newEmbossedText( "X-Lrg", 0, 0, myData.menuFont, 20 ) sizeLargeText.anchorX, sizeLargeText.anchorY = 0, .5 sizeLargeText:setFillColor( 255/255 ) sizeLargeText.x = sizeText.x + 220 sizeLargeText.y = sizeText.y + 2 sizeLargeText.alpha = 0 widgetGroup:insert(sizeLargeText) checkBoxLarge = widget.newSwitch { left = 270, top = sizeText.y - 20, style = "checkbox", id = "Large", initialSwitchState = false, onPress = onSizePress } checkBoxLarge.alpha = 0 widgetGroup:insert(checkBoxLarge)

This is code to transition them in (there are more switches, but the code is the same for all of them.):

--Transition out the list, transition in the item selected text and the back button transition.to( list, { x = - list.contentWidth, time = 400, transition = easing.Quad } ) --transition.to( itemSelected, { x = display.contentCenterX,y = 60, time = 0, transition = easing.outExpo } ) transition.to( backButton, { alpha = 1.0, time = 400, transition = easing.outExpo } ) transition.to( menuButton, { alpha = 0, time = 400, transition = easing.outExpo } ) transition.to( sizeText, { alpha = sizeTextOption, time = 400, transition = easing.outExpo } ) transition.to( sizeSmallText, { alpha = sizeTextOption, time = 400, transition = easing.outExpo } ) transition.to( checkBoxSmall, { alpha = sizeSmallOption, time = 400, transition = easing.outExpo } ) transition.to( sizeRegularText, { alpha = sizeRegularOption, time = 400, transition = easing.outExpo } ) transition.to( checkBoxRegular, { alpha = sizeRegularOption, time = 400, transition = easing.outExpo } ) transition.to( sizeLargeText, { alpha = sizeLargeOption, time = 400, transition = easing.outExpo } ) transition.to( checkBoxLarge, { alpha = sizeLargeOption, time = 400, transition = easing.outExpo } )

Can you post your “onSizePress” code as well? Remember to use the <lua> encoding to make sure it formats correctly.

Here you go.

local function onSizePress( event )&nbsp; &nbsp; local switch = event.target &nbsp; &nbsp; --local phase = event.phase local response = switch.id.." is on: "..tostring( switch.isOn ) &nbsp; &nbsp;print( response ) &nbsp; &nbsp; &nbsp; print (sizeCost) &nbsp; &nbsp; &nbsp; print(itemMaths) &nbsp; &nbsp; &nbsp; print("Pressed") &nbsp; &nbsp; &nbsp; &nbsp; if (switch.id == "Small") then&nbsp; &nbsp; &nbsp; checkBoxSmall:setState{ isOn = true } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = false } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = false } &nbsp; &nbsp; sizeCost = myData.coffeeSmall &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; elseif (switch.id == "Regular") then &nbsp; &nbsp; checkBoxSmall:setState{ isOn = false } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = true } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = false } &nbsp; &nbsp; sizeCost = myData.coffeeRegular &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; elseif (switch.id == "Large" ) then &nbsp; &nbsp; checkBoxSmall:setState{ isOn = false } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = false } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = true } &nbsp; &nbsp; sizeCost = myData.coffeeLarge &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updateOrder() end

Anyone got any ideas about what might be wrong?

I tried changing to ‘onRelease’ and it still doesn’t work.

I tried the ‘old’ version which uses Graphics 1.0 compatibility mode and the switches work perfectly, but the table does not display correctly after I press the back button. I can’t seem to fix that either, because I could build that version and use it, whilst trying to find a fix for this one.

Hi @ray90,

This appears like you’re trying to create a “radio button” set, where other buttons turn off when another is pressed. However, you’re using “checkbox” style switches, not “radio”, and then doing all the manual :setState() operations. I suggest you just make this into a radio button set, and if they’re inserted into the same display group, they should behave as a proper set and you won’t need to bother with all of the state changes.

See the documentations and example here:

http://docs.coronalabs.com/api/library/widget/newSwitch.html

Best regards,

Brent

Thank you Brent,

The ‘radio buttons’ work perfectly!

Although it doesn’t suit every choice within my App. As some of the choices relied on a ‘checkbox’.

Being that the ‘radio buttons’ required a new/separate group to work, I tried putting each individual ‘checkbox’ into it’s own group and now they work as they should. Not sure where that bug comes from but may explain why I couldn’t figure things out.

I should add that I am using ‘Director’ for my scene management. That may or may not be important!

Ray

Can you post your “onSizePress” code as well? Remember to use the <lua> encoding to make sure it formats correctly.

Here you go.

local function onSizePress( event )&nbsp; &nbsp; local switch = event.target &nbsp; &nbsp; --local phase = event.phase local response = switch.id.." is on: "..tostring( switch.isOn ) &nbsp; &nbsp;print( response ) &nbsp; &nbsp; &nbsp; print (sizeCost) &nbsp; &nbsp; &nbsp; print(itemMaths) &nbsp; &nbsp; &nbsp; print("Pressed") &nbsp; &nbsp; &nbsp; &nbsp; if (switch.id == "Small") then&nbsp; &nbsp; &nbsp; checkBoxSmall:setState{ isOn = true } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = false } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = false } &nbsp; &nbsp; sizeCost = myData.coffeeSmall &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; elseif (switch.id == "Regular") then &nbsp; &nbsp; checkBoxSmall:setState{ isOn = false } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = true } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = false } &nbsp; &nbsp; sizeCost = myData.coffeeRegular &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; elseif (switch.id == "Large" ) then &nbsp; &nbsp; checkBoxSmall:setState{ isOn = false } &nbsp; &nbsp; checkBoxRegular:setState{ isOn = false } &nbsp; &nbsp; checkBoxLarge:setState{ isOn = true } &nbsp; &nbsp; sizeCost = myData.coffeeLarge &nbsp; &nbsp; cupSize = " " .. switch.id &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updateOrder() end

Anyone got any ideas about what might be wrong?

I tried changing to ‘onRelease’ and it still doesn’t work.

I tried the ‘old’ version which uses Graphics 1.0 compatibility mode and the switches work perfectly, but the table does not display correctly after I press the back button. I can’t seem to fix that either, because I could build that version and use it, whilst trying to find a fix for this one.

Hi @ray90,

This appears like you’re trying to create a “radio button” set, where other buttons turn off when another is pressed. However, you’re using “checkbox” style switches, not “radio”, and then doing all the manual :setState() operations. I suggest you just make this into a radio button set, and if they’re inserted into the same display group, they should behave as a proper set and you won’t need to bother with all of the state changes.

See the documentations and example here:

http://docs.coronalabs.com/api/library/widget/newSwitch.html

Best regards,

Brent

Thank you Brent,

The ‘radio buttons’ work perfectly!

Although it doesn’t suit every choice within my App. As some of the choices relied on a ‘checkbox’.

Being that the ‘radio buttons’ required a new/separate group to work, I tried putting each individual ‘checkbox’ into it’s own group and now they work as they should. Not sure where that bug comes from but may explain why I couldn’t figure things out.

I should add that I am using ‘Director’ for my scene management. That may or may not be important!

Ray