on/off switch

Actually, i am a little bit confused on how to used onoffswitch. 

onOffSwitch = widget.newSwitch

{

   left = 80,

   top = -40,

   initialSwitchState = true,

   onPress = onOffSwitchListener,

   onRelease = onOffSwitchListener,

}

If anyone can help me, how to work on it if I want to play sound during on switch and off if switch that widget.

local function onOffSwitchEvent( event )

– Update the status box text

–statusText.text = "On/Off Switch\nIs on?: " … tostring( event.target.isOn )

local onOffSwitch = event.target

if (onOffSwitch.isOn) then

audio.resume( popSound )

else

audio.pause( popSound )

end

end

It is the correct way or there is another way to work on it?

Hi @hasanah920215,

You should not use both a “onPress” AND “onRelease” parameter which points to the “onOffSwitchEvent” function. Choose either one (but not both) depending on whether you want the event to occur when the switch is first pressed, or after the switch is released. Personally, I prefer the “onRelease” for most usage cases, because it indicates that the user is finished manipulating the switch.

Brent

musicSwitch = widget.newSwitch

{

   left = 80,

   top = -40,

   initialSwitchState = true,

   onRelease = musicSwitchListener,

}

something like this right? but still didn’t work

local function onmusicSwitchRelease( event )

if event.target.isOn == true then

audio.play( popSound )

elseif event.target.isOn == false then 

audio.pause()

end

did i miss anything? 

Hi @hasanah920215,

You aren’t using the same function name in the switch constructor to match the actual function. For the widget you have “musicSwitchListener” but the function name is “onmusicSwitchRelease”. So, these two aspects do not recognize each other, and you will get no response.

Brent

Hi @hasanah920215,

You should not use both a “onPress” AND “onRelease” parameter which points to the “onOffSwitchEvent” function. Choose either one (but not both) depending on whether you want the event to occur when the switch is first pressed, or after the switch is released. Personally, I prefer the “onRelease” for most usage cases, because it indicates that the user is finished manipulating the switch.

Brent

musicSwitch = widget.newSwitch

{

   left = 80,

   top = -40,

   initialSwitchState = true,

   onRelease = musicSwitchListener,

}

something like this right? but still didn’t work

local function onmusicSwitchRelease( event )

if event.target.isOn == true then

audio.play( popSound )

elseif event.target.isOn == false then 

audio.pause()

end

did i miss anything? 

Hi @hasanah920215,

You aren’t using the same function name in the switch constructor to match the actual function. For the widget you have “musicSwitchListener” but the function name is “onmusicSwitchRelease”. So, these two aspects do not recognize each other, and you will get no response.

Brent