how to test to see if widget switch is on or off

how do you test to see if a widget switch is on or off???

here is what i have

[lua]

local function onsfxSwitchRelease()

–want to test if the switch is on or off and save the result to my icebox here

return true

end

function scene:createScene( event )

    local group = self.view

        -----------------------------------------------------------------------------

        –      CREATE display objects and add them to ‘group’ here.

        –      Example use-case: Restore ‘group’ from previously saved state.

        –        Use following format: group:insert( Name )

        -----------------------------------------------------------------------------

        

        – display a background image

    local background = display.newImageRect( “background3.png”, display.contentWidth, display.contentHeight )

    background:setReferencePoint( display.TopLeftReferencePoint )

    background.x, background.y = 0, 0

    local myRoundedRect = display.newRoundedRect(134, 134, 500, 748, 12)

    myRoundedRect.strokeWidth = 8

    myRoundedRect:setFillColor(214, 214, 214)

    myRoundedRect:setStrokeColor(111, 111, 111)

    local sfxSwitch = widget.newSwitch

    {

    left = 500,

    top = 356,

    initialSwitchState = true,

    onPress = onsfxSwitchPress,

    onRelease = onsfxSwitchRelease,

    }

    

    group:insert( background )

    group:insert( myRoundedRect )

    group:insert( sfxSwitch )

end

[/lua]

i marked where i want it to test the value and save it to my icebox

any help appreciated.

Not sure if you can get the state, but here is how to set it:  http://docs.coronalabs.com/api/type/SwitchWidget/setState.html

You may just have to create your own solution to get the state.

ok, that’s the kind of page i was looking for.

what is the standard way of having a user say yes or no to a question and then have it do something if yes, or something else if no?

if there isn’t a standard way, how would address the switch from inside the function? just sfxSwitch?

One thing, you’re switch is local to your createScene() function.  You need to probably take the local off of it in createScene() and put a:

local sfxSwitch

at the top of your program.  According to the docs:

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

There is a .isOn attribute for the switch.  See the example code where in the event handler they print the value of event.target.isOn?  event.target is the switch and if you do what I said above where you can have a reference to it outside of createScene you should be able to test for

sfxSwitch.isOn

this is what i came up with:

[lua]

local sfxSwitch

local leftySwitch

local function onsfxSwitchRelease( event )

if event.target.isOn == true then

–code here

elseif event.target.isOn == false then

–code here

end

return true

end

function scene:createScene( event )
local group = self.view

sfxSwitch = widget.newSwitch
{
left = 500,
top = 356,
initialSwitchState = true,
onRelease = onsfxSwitchRelease( event ),
}
–in group here

end

[/lua]

but i get an error saying “attempt to index field ‘target’ (a nil value)”

Line #18

onRelease = onsfxSwitchRelease( event ),

don’t need to have the ( event ) attached to the end of onsfxSwitchRelease.   Take that off and you won’t get the error, however, I get no indication the event is captured when I use ‘onEvent’ or ‘onRelease’

try this instead at line 18:

onPress = onsfxSwitchRelease,

it works.

Good luck.

Line #18

onRelease = onsfxSwitchRelease( event ),

 

don’t need to have the ( event ) attached to the end of onsfxSwitchRelease.   Take that off and you won’t get the error, however, I get no indication the event is captured when I use ‘onEvent’ or ‘onRelease’

 

try this instead at line 18:

 

onPress = onsfxSwitchRelease,

 

it works.

 

Good luck.

ok, changed to this and it still doesn’t work.

[lua]

local sfxSwitch

local function onsfxSwitchRelease()

    

    local switch = event.target  --THIS IS LINE 42

    

    if switch.isOn == true then

    

    local rect = display.newRoundedRect(245, 25, 80, 80, 12)

    rect.strokeWidth = 3

    rect:setFillColor(255, 255, 255)

    rect:setStrokeColor(0, 0, 0)

    

    

    end

    

    return true

end

function scene:createScene( event )

    local group = self.view

sfxSwitch = widget.newSwitch

    {

    left = 500,

    top = 356,

    initialSwitchState = true,

    onPress = onsfxSwitchRelease(),

    }

    

–group stuff

end

[/lua]

im getting …42: attempt to index global ‘event’ (a nil value) now…

i also tried if sfxSwitch.isOn == true then like Mr. Miracle said, gives me an error about trying to index a nil value

 local function onsfxSwitchRelease(event) local switch = event.target if event.phase == "ended" then if switch.isOn == true then local rect = display.newRoundedRect(245, 25, 80, 80, 12) rect.strokeWidth = 3 rect:setFillColor(255, 255, 255) rect:setStrokeColor(0, 0, 0) end end return true end function scene:createScene( event ) local group = self.view sfxSwitch = widget.newSwitch { left = 500, top = 356, initialSwitchState = true, onPress = onsfxSwitchRelease, } --group stuff end

@Jon- thanks for the reply, but i got it sorted out by moving the whole local function into the create scene function, which i don’t see a problem with (this remains to be seen in the long run).  I also added a part where if it was false it would display a different color rect, so it seems to work alright.  Now i have to add in my ICE code :wacko:…

icero,

The code changes I noted earlier would and do work.  I noticed in the code you listed in your response to my suggestion, that you just had 2 things mis-typed from what I had noted.  Those 2 things made it fail.

Line 5 : 

local function onsfxSwitchRelease() – won’t work

local function onsfxSwitchRelease( event ) – works … must have ‘event’ inside the ’ ( ) ’

Line 33 : 

onPress = onsfxSwitchRelease(), – won’t work

onPress = onsfxSwitchRelease, – works … remove the ’ ( ) ’ , cannot have them there

So,

when you call the onPress, do not use ’ ( ) ', just the name of the function  ‘onsfxSwitchRelease’

when you are going to use  ‘event’ inside the function, be sure it is in the  ’ (  ) ’

I hope you get it all running smooth!

oh, i see what you were saying now, i pretty much did that same thing you suggested, but put it in the main create function to get it to work

I got ICE up and running, and it is saving the settings alright, and reading them for now.  I only have one more major problem with my game at this point and that is users can drag their finger off a button widget if the accidentally click on it and it won’t register a onRelease event(which it is critical it does).  You guys have any idea on how to fix that???

Not sure if you can get the state, but here is how to set it:  http://docs.coronalabs.com/api/type/SwitchWidget/setState.html

You may just have to create your own solution to get the state.

ok, that’s the kind of page i was looking for.

what is the standard way of having a user say yes or no to a question and then have it do something if yes, or something else if no?

if there isn’t a standard way, how would address the switch from inside the function? just sfxSwitch?

One thing, you’re switch is local to your createScene() function.  You need to probably take the local off of it in createScene() and put a:

local sfxSwitch

at the top of your program.  According to the docs:

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

There is a .isOn attribute for the switch.  See the example code where in the event handler they print the value of event.target.isOn?  event.target is the switch and if you do what I said above where you can have a reference to it outside of createScene you should be able to test for

sfxSwitch.isOn

this is what i came up with:

[lua]

local sfxSwitch

local leftySwitch

local function onsfxSwitchRelease( event )

if event.target.isOn == true then

–code here

elseif event.target.isOn == false then

–code here

end

return true

end

function scene:createScene( event )
local group = self.view

sfxSwitch = widget.newSwitch
{
left = 500,
top = 356,
initialSwitchState = true,
onRelease = onsfxSwitchRelease( event ),
}
–in group here

end

[/lua]

but i get an error saying “attempt to index field ‘target’ (a nil value)”

Line #18

onRelease = onsfxSwitchRelease( event ),

don’t need to have the ( event ) attached to the end of onsfxSwitchRelease.   Take that off and you won’t get the error, however, I get no indication the event is captured when I use ‘onEvent’ or ‘onRelease’

try this instead at line 18:

onPress = onsfxSwitchRelease,

it works.

Good luck.

Line #18

onRelease = onsfxSwitchRelease( event ),

 

don’t need to have the ( event ) attached to the end of onsfxSwitchRelease.   Take that off and you won’t get the error, however, I get no indication the event is captured when I use ‘onEvent’ or ‘onRelease’

 

try this instead at line 18:

 

onPress = onsfxSwitchRelease,

 

it works.

 

Good luck.

ok, changed to this and it still doesn’t work.

[lua]

local sfxSwitch

local function onsfxSwitchRelease()

    

    local switch = event.target  --THIS IS LINE 42

    

    if switch.isOn == true then

    

    local rect = display.newRoundedRect(245, 25, 80, 80, 12)

    rect.strokeWidth = 3

    rect:setFillColor(255, 255, 255)

    rect:setStrokeColor(0, 0, 0)

    

    

    end

    

    return true

end

function scene:createScene( event )

    local group = self.view

sfxSwitch = widget.newSwitch

    {

    left = 500,

    top = 356,

    initialSwitchState = true,

    onPress = onsfxSwitchRelease(),

    }

    

–group stuff

end

[/lua]

im getting …42: attempt to index global ‘event’ (a nil value) now…

i also tried if sfxSwitch.isOn == true then like Mr. Miracle said, gives me an error about trying to index a nil value

 local function onsfxSwitchRelease(event) local switch = event.target if event.phase == "ended" then if switch.isOn == true then local rect = display.newRoundedRect(245, 25, 80, 80, 12) rect.strokeWidth = 3 rect:setFillColor(255, 255, 255) rect:setStrokeColor(0, 0, 0) end end return true end function scene:createScene( event ) local group = self.view sfxSwitch = widget.newSwitch { left = 500, top = 356, initialSwitchState = true, onPress = onsfxSwitchRelease, } --group stuff end