[code]
function newOnOffSwitch( x, y, startingValue )
– Staring value can either be “on” or “off”
print(“new on off switch”)
– declare groups
local onOffSwitch = display.newGroup() --> holds entire switch
– START DEFAULT VAUES:
if not x then x = 0;print(“not x”); end
if not y then y = 0;print(“not y”); end
if not startingValue then
onOffSwitch.value = “on”
print(“s v on”)
else
–onOffSwitch.value = “off”
onOffSwitch.value = startingValue
print("starting value ",startingValue)
end
– // END DEFAULT VALUES
– create the actual on/off switch graphic
local onOffSwitchGraphic = display.newImageRect( “coronaui_onoffslider.png”, 156, 36 )
– if onOffSwitch is set to “off”, change it’s position
if onOffSwitch.value == “off” then
onOffSwitchGraphic.x = -54
print(“onoffv switch off”)
end
onOffSwitchGraphic.prevPos = onOffSwitchGraphic.x
onOffSwitch:insert( onOffSwitchGraphic )
– create a bitmap mask and set it on the whole group
local onOffMask = graphics.newMask( “coronaui_onoffslidermask.png” )
onOffSwitch:setMask( onOffMask )
onOffSwitch.maskScaleX, onOffSwitch.maskScaleY = .5, .5
– START TOUCH LISTENER FOR ACTUAL ON/OFF SLIDER:
function onOffSwitchGraphic:touch( event )
print(“event”)
if event.phase == “began” then
print(“touch event”)
display.getCurrentStage():setFocus( self )
self.isFocus = true
self.delta = 0
elseif( self.isFocus ) then
if event.phase == “moved” then
print(“moved”)
–self.x = event.x - event.xStart
self.delta = event.x - self.prevPos
self.prevPos = event.x
self.x = self.x + self.delta
if self.x < -54 then self.x = -54; end
if self.x > 0 then self.x = 0; end
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
self.isFocus = false
print(“ended”)
if self.tween then transition.cancel( self.tween ); self.tween = nil; end
local assessSwitch = function()
if self.x > -23 then
self.parent.value = “on”
hswitch = “on”
print(“on”)
propertyBag:setProperty(“hswitch”, “on”)
propertyBag:SaveToFile()
else
self.parent.value = “off”
hswitch = “off”
print(“off”)
propertyBag:setProperty(“hswitch”, “off”)
propertyBag:SaveToFile()
end
end
if self.parent.value == “off” then
self.tween = transition.to( self, { time=200, x=0, transition=easing.outQuad, onComplete=assessSwitch } )
else
self.tween = transition.to( self, { time=200, x=-54, transition=easing.outQuad, onComplete=assessSwitch } )
end
end
end
end
onOffSwitchGraphic:addEventListener( “touch”, onOffSwitchGraphic )
– // END TOUCH LISTENER FOR ON/OFF SLIDER
– finally, position entire group:
onOffSwitch.x = x
onOffSwitch.y = y
qgroup2:insert(onOffSwitch)
return onOffSwitch
end
hintlabel = display.newText (“hint text”, centre-32, 5, “Helvetica”, 18 );
print("hswitch ",hswitch)
hswitch = propertyBag:getProperty (“hswitch”)
if hswitch == “on” then
print(“on”)
newOnOffSwitch( centre+28, 50, “on” )
–propertyBag:setProperty (“hswitch”, “on”)
–propertyBag:SaveToFile()
elseif hswitch == “off” then
print(“off”)
newOnOffSwitch( centre+28, 50, “off” )
–propertyBag:setProperty (“hswitch”, “off”)
–propertyBag:SaveToFile()
end
qgroup2:insert(hintlabel)
–end of hint switch
[/code] [import]uid: 2131 topic_id: 13825 reply_id: 313825[/import]