Just Found That My Code Isn't Resetting myData Value When The Scene Is Changed?

Hi guys,

So I have a few scenes and on each scene I have a list of radio buttons, and when one is clicked then onpress of the next button it sets a value in myData.lua now the issue is once one button has set the value on any of the other pages if a different radio button is clicked it doesn’t change the value it stays as the first on clicked.

Here are two of my scenes, really need help.

[lua]


– incidentReportingPg1.lua


local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

local function btnPrevious( event )

    composer.gotoScene( “incidentReportingPg6”, { effect=“slideRight”, time=800} )

end

function scene:create( event )

local sceneGroup = self.view

print( myData.IRb4 )

–bg

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 ) – white

sceneGroup:insert( bg )

–titleHeader

local titleHeader = display.newImage( “titleHeader.png” )

titleHeader:translate( 160, -22 )

–title

local title = display.newText( “Incident Reporting”, 0, 0, native.systemFont, 32 )

title:setFillColor( 0 ) – black

title.x = display.contentWidth * 0.5

title.y = -25

–textPage

local textPage = display.newText(“Page 7”, 

280,130, display.contentWidth -25 , display.contentHeight * 0.5, native.systemFont, 15 )

textPage:setFillColor( 0, 0, 0 )

–textWhatWas

local textWhatWas = display.newText(“What was the cause of the incident? (1 of 5)”, 

172, 150, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textWhatWas:setFillColor( 0, 0, 0 )

sceneGroup:insert( textWhatWas )

–textSlips

local textSlips = display.newText(“Slips, trip or fall on the same level”, 

212, 219, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textSlips:setFillColor( 0, 0, 0 )

sceneGroup:insert( textSlips )

–textFallFrom

local textFallFrom = display.newText(“Fall from Height”, 

212, 280, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textFallFrom:setFillColor( 0, 0, 0 )

sceneGroup:insert( textFallFrom )

–textBoxHeight

local textBoxHeight = native.newTextField( 200, 166, 75, 40 )

textBoxHeight.size = 20

textBoxHeight:addEventListener( “userInput”, textBoxHeight )

textBoxHeight.inputType = “number”

sceneGroup:insert( textBoxHeight )

–textInMeters

local textInMeters = display.newText(“In Meters”, 

410, 280, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textInMeters:setFillColor( 0, 0, 0 )

sceneGroup:insert( textInMeters )

–textInjured

local textInjured = display.newText(“Injured whilst handling, lifting or carrying”, 

212, 341, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textInjured:setFillColor( 0, 0, 0 )

sceneGroup:insert( textInjured )

–textHit

local textHit = display.newText(“Hit by an object”, 

212, 402, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textHit:setFillColor( 0, 0, 0 )

sceneGroup:insert( textHit )

–textContact

local textContact = display.newText(“Contact with an object”, 

212, 463, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textContact:setFillColor( 0, 0, 0 )

sceneGroup:insert( textContact )

–textMore

local textMore = display.newText("**More options are available on the next page**", 

180, 500, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textMore:setFillColor( 0, 0, 0 )

sceneGroup:insert( textMore )

– Image sheet options and declaration

local options = {

    width = 26,

    height = 26,

    numFrames = 2,

    sheetContentWidth = 26,

    sheetContentHeight = 52

}

local checkboxSheet = graphics.newImageSheet( “checkboxSheet.png”, options )

– Create a group for the radio button set

local radioGroup = display.newGroup()

– Create two associated radio buttons (inserted into the same display group)

local radioButton1 = widget.newSwitch

{

    left = 20,

    top = 92,

    style = “radio”,

    id = “RadioButton1”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton1 )

sceneGroup:insert( radioButton1 )

local radioButton2 = widget.newSwitch

{

    left = 20,

    top = 153,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton2 )

sceneGroup:insert( radioButton2 )

local radioButton3 = widget.newSwitch

{

    left = 20,

    top = 214,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton3 )

sceneGroup:insert( radioButton3 )

local radioButton4 = widget.newSwitch

{

    left = 20,

    top = 275,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton4 )

sceneGroup:insert( radioButton4 )

local radioButton5 = widget.newSwitch

{

    left = 20,

    top = 336,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton5 )

sceneGroup:insert( radioButton5 )

local function OnbtnNext( event )

      if event.phase == “ended” then

            local radioOn = false

            local textFilled = true

            myData.fallHeight = textBoxHeight.text

            if radioButton1.isOn == true then

            myData.IRb4 = textSlips.text

          end

            if radioButton2.isOn == true then

           myData.IRb4 = textFallFrom.text

                  radioOn = true

            end

            if radioButton3.isOn == true then

           myData.IRb4 = textInjured.text

            end

            if radioButton4.isOn == true then

           myData.IRb4 = textHit.text

            end

            

            if radioButton5.isOn == true then

           myData.IRb4 = textContact.text        

            end

      if textBoxHeight.text == “” or textBoxHeight.text == nil then

                  textFilled = false

           end

            if radioOn == true and textFilled then

                   composer.gotoScene( “incidentReportingPg8”, { effect=“slideLeft”, time=800,} )

            end

            if radioOn == true and textFilled == false then

                             local msg

                  if textFilled == false then

                        msg = “No input in textbox”

                  end

                  local alert = native.showAlert( “Warning”, "ERROR: "…msg, { “OK” }, onComplete)

              end

        if radioOn == false and textFilled == false then

        composer.gotoScene( “incidentReportingPg8”, { effect=“slideLeft”, time=800,} )

        end

      end

      return true

end

–btnPrevious

local btnPrevious = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnPrevious.png”,

   onEvent = btnPrevious

}

btnPrevious.x = 84

btnPrevious.y = 445

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = OnbtnNext

}

btnNext.x = 235

btnNext.y = 445

–sceneGroup

sceneGroup:insert( titleHeader )

sceneGroup:insert( title )

sceneGroup:insert( textPage )

sceneGroup:insert( btnNext )

sceneGroup:insert( btnPrevious )

end 

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

[lua]


– incidentReportingPg1.lua


local composer = require( “composer” )

local scene = composer.newScene()

local widget = require( “widget” )

local myData = require( “myData” )

local function btnNext( event )

    composer.gotoScene( “incidentReportingPg9”, { effect=“slideLeft”, time=800} )

end

local function btnPrevious( event )

    composer.gotoScene( “incidentReportingPg7”, { effect=“slideRight”, time=800} )

end

function scene:create( event )

local sceneGroup = self.view

print( myData.IRb4 )

–bg

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )

bg.anchorX = 0

bg.anchorY = 0

bg:setFillColor( 0.9, 0.9, 0.9 ) – white

sceneGroup:insert( bg) 

–titleHeader

local titleHeader = display.newImage( “titleHeader.png” )

titleHeader:translate( 160, -22 )

–title

local title = display.newText( “Incident Reporting”, 0, 0, native.systemFont, 32 )

title:setFillColor( 0 ) – black

title.x = display.contentWidth * 0.5

title.y = -25

–textPage

local textPage = display.newText(“Page 8”, 

280, 130, display.contentWidth -25 , display.contentHeight * 0.5, native.systemFont, 15 )

textPage:setFillColor( 0, 0, 0 )

–textWhatWas

local textWhatWas = display.newText(“What was the cause of the incident? (2 of 5)”, 

172, 150, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 20 )

textWhatWas:setFillColor( 0, 0, 0 )

sceneGroup:insert( textWhatWas )

–textVehicleOff

local textVehicleOff = display.newText(“Vehicle road traffic incident (off campus)”, 

212, 220, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textVehicleOff:setFillColor( 0, 0, 0 )

sceneGroup:insert( textVehicleOff )

–textVehicleOn

local textVehicleOn = display.newText(“Vehicle road traffic incident (on campus)”, 

212, 281, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textVehicleOn:setFillColor( 0, 0, 0 )

sceneGroup:insert( textVehicleOn )

–textIncidentInvolving

local textIncidentInvolving = display.newText(“Incident involving a bicycle(s)”, 

212, 342, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textIncidentInvolving:setFillColor( 0, 0, 0 )

sceneGroup:insert( textIncidentInvolving )

–textAssaulted

local textAssaulted = display.newText(“Assaulted by another person”, 

212, 403, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textAssaulted:setFillColor( 0, 0, 0 )

sceneGroup:insert( textAssaulted )

–textWorkRelated

local textWorkRelated = display.newText(“Work related upper limb disorder (e.g. RSI)”, 

212, 464, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textWorkRelated:setFillColor( 0, 0, 0 )

sceneGroup:insert( textWorkRelated )

–textMore

local textMore = display.newText("**More options are available on the next page**", 

180, 500, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textMore:setFillColor( 0, 0, 0 )

sceneGroup:insert( textMore )

– Image sheet options and declaration

local options = {

    width = 26,

    height = 26,

    numFrames = 2,

    sheetContentWidth = 26,

    sheetContentHeight = 52

}

local checkboxSheet = graphics.newImageSheet( “checkboxSheet.png”, options )

– Create a group for the radio button set

local radioGroup = display.newGroup()

– Create two associated radio buttons (inserted into the same display group)

local radioButton1 = widget.newSwitch

{

    left = 20,

    top = 92,

    style = “radio”,

    id = “RadioButton1”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton1 )

sceneGroup:insert( radioButton1 )

local radioButton2 = widget.newSwitch

{

    left = 20,

    top = 153,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton2 )

sceneGroup:insert( radioButton2 )

local radioButton3 = widget.newSwitch

{

    left = 20,

    top = 214,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton3 )

sceneGroup:insert( radioButton3 )

local radioButton4 = widget.newSwitch

{

    left = 20,

    top = 275,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton4 )

sceneGroup:insert( radioButton4 )

local radioButton5 = widget.newSwitch

{

    left = 20,

    top = 336,

    style = “radio”,

    id = “RadioButton2”,

    width = 26,

    height = 26,

    onPress = onSwitchPress,

    sheet = checkboxSheet,

    frameOff = 1,

    frameOn = 2

}

radioGroup:insert( radioButton5 )

sceneGroup:insert( radioButton5 )

local function OnbtnNext( event )

      if event.phase == “ended” then

            local radioOn = false

            if radioButton1.isOn == true then

            myData.IRb4 = textVehicleOff.text

                  radioOn = true

          end

            if radioButton2.isOn == true then

            myData.IRb4 = textVehicleOn.text

                  radioOn = true

            end

            if radioButton3.isOn == true then

            myData.IRb4 = textIncidentInvolving.text

                  radioOn = true

            end

            if radioButton4.isOn == true then

            myData.IRb4 = textAssaulted.text

                  radioOn = true

            end

            if radioButton5.isOn == true then

            myData.IRb4 = textWorkRelated.text

                  radioOn = true

            end

            else

                   composer.gotoScene( “incidentReportingPg9”, { effect=“slideLeft”, time=800,} )

           

       

      end

      return true

end

–btnPrevious

local btnPrevious = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnPrevious.png”,

   onEvent = btnPrevious

}

btnPrevious.x = 84

btnPrevious.y = 445

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = btnNext

}

btnNext.x = 235

btnNext.y = 445

–sceneGroup

sceneGroup:insert( titleHeader )

sceneGroup:insert( title )

sceneGroup:insert( textPage )

sceneGroup:insert( btnNext )

sceneGroup:insert( btnPrevious )

end 

function scene:show( event )

local sceneGroup = self.view

end

function scene:hide( event )

local sceneGroup = self.view

end

function scene:destroy( event )

local sceneGroup = self.view

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

Thanks again,

Matt.

What does myData.lua look like?

Sorry looks like this…

[lua]

–my global space

local myData = {}

–Incident Reporting

–P2

myData.dateDay = “00”

myData.dateMonth = “00”

myData.dateYear = “00”

myData.timeHour = “00”

myData.timeMinute = “00”

myData.IRb1 = “N/A”

–P3

myData.incidentLocation = “N/A”

myData.IRb2 = “N/A”

–P4/P5

myData.IRb3 = “N/A”

–P6

myData.injuryOccurred = “N/A”

–P7/P8/P9/P10

myData.fallHeight = “N/A”

myData.textOther = “N/A”

myData.IRb4 = “N/A”

–Risk Assessment

myData.risk1 = “N/A”

return myData

[/lua]

Thanks,

Matt.

If more than one radio button is set to on, myData.IRb4 will be set to that of the highest numbered button. Is that what you want to happen?

From what I can make out, you’re not purging the previous scene when you switch to another scene. So the radio buttons will remain in the same positions as you left them, and if the radio button that is first set to on is higher numbered than the one you later set to on when you revisit the scene, myData.IRb4 will remain unchanged.

Hi again,

Thanks for the help here, how would you suggest I go about fixing this issue?

Matt.

I’d suggest setting your id field between 1-6 when you create each radio button. Within OnBtnNext, use event.target.isOn to determine if the button was switched on, and event.target.id to determine which button has been changed.

You can then use a table to determine what to set myData.IRb4 to. I can’t give a full example right now as my machine is running a macro and constantly accessing the clipboard, so I can’t copy your code into an editor.

Lookup table:

[lua]

local txt = {text.vehicleOffText, text.vehicleOnText} – fill in the rest

myData.IRb4 = txt[event.target.id]

 [/lua]

I guess you’d still then have to loop through your buttons to check whether all of them are off. I’d put all the buttons into a table, so you end up with radioButton[1], radioButton[2] etc - you can then easily loop through them and check their status - if any of them are on, set radiOn to true.

Hi,

This is a tad too heavy for my current lua skill level I think, would it be possible for you to help me when your machine has finished running your macro. I have been tring to work this out and have had no luck. I would appreciate this alot. 

Thanks for the help,

Matt.

What does myData.lua look like?

Sorry looks like this…

[lua]

–my global space

local myData = {}

–Incident Reporting

–P2

myData.dateDay = “00”

myData.dateMonth = “00”

myData.dateYear = “00”

myData.timeHour = “00”

myData.timeMinute = “00”

myData.IRb1 = “N/A”

–P3

myData.incidentLocation = “N/A”

myData.IRb2 = “N/A”

–P4/P5

myData.IRb3 = “N/A”

–P6

myData.injuryOccurred = “N/A”

–P7/P8/P9/P10

myData.fallHeight = “N/A”

myData.textOther = “N/A”

myData.IRb4 = “N/A”

–Risk Assessment

myData.risk1 = “N/A”

return myData

[/lua]

Thanks,

Matt.

If more than one radio button is set to on, myData.IRb4 will be set to that of the highest numbered button. Is that what you want to happen?

From what I can make out, you’re not purging the previous scene when you switch to another scene. So the radio buttons will remain in the same positions as you left them, and if the radio button that is first set to on is higher numbered than the one you later set to on when you revisit the scene, myData.IRb4 will remain unchanged.

Hi again,

Thanks for the help here, how would you suggest I go about fixing this issue?

Matt.

I’d suggest setting your id field between 1-6 when you create each radio button. Within OnBtnNext, use event.target.isOn to determine if the button was switched on, and event.target.id to determine which button has been changed.

You can then use a table to determine what to set myData.IRb4 to. I can’t give a full example right now as my machine is running a macro and constantly accessing the clipboard, so I can’t copy your code into an editor.

Lookup table:

[lua]

local txt = {text.vehicleOffText, text.vehicleOnText} – fill in the rest

myData.IRb4 = txt[event.target.id]

 [/lua]

I guess you’d still then have to loop through your buttons to check whether all of them are off. I’d put all the buttons into a table, so you end up with radioButton[1], radioButton[2] etc - you can then easily loop through them and check their status - if any of them are on, set radiOn to true.

Hi,

This is a tad too heavy for my current lua skill level I think, would it be possible for you to help me when your machine has finished running your macro. I have been tring to work this out and have had no luck. I would appreciate this alot. 

Thanks for the help,

Matt.