Simple widget Slider

I would like to star using sliders…

I copy this code from Corona

-- Slider listener local function sliderListener( event )     print( "Slider at " .. event.value .. "%" ) end -- Create the widget local slider = widget.newSlider {     top = 200,     left = 50,     width = 400,     value = 10,  -- Start slider at 10% (optional)     listener = sliderListener }

I see a grey bar with the Blue line for the slider…

When I move the circle I see in the “terminal”


2014-07-03 12:51:40.235 Corona Simulator[96836:507] Slider at 56%


So far so good…

Now How Do I make an image move, or play a sound or do something with the slider?

something like this…

-- Slider listener local function sliderListener( event )     print( "Slider at " .. event.value .. "%" )          if slider is at 56% then         audio.play(soundBig)     elseif slider is at 80% then         audio.play(soundSmall)     end      end

I know is not the right code but I guess I have to do something like that

I just want to find out what?

or if event.value is from 34% to 44% then

I think you have the idea…

Thanks for your help

In the listener function you get passed a table called event.  One of the members of that table is called value:

event.value

This is a number from 0 to 100 that represents where the slider is.  You would do something like:

local function sliderListener( event ) &nbsp;&nbsp;&nbsp; print( "Slider at " .. event.value .. "%" ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if event.value \> 55 and event.value \< 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(soundBig) &nbsp;&nbsp;&nbsp; elseif event.value \>= 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(soundSmall) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; &nbsp; end

Thanks Rob… That was Easy…

it WORKS!!!


what about the IMAGE or CUSTOM slider

local widget = require( "widget" ) -- Slider listener local function sliderListener( event ) print( "Slider at " .. event.value .. "%" ) end -- Image sheet options and declaration local options = { frames = { { x=0, y=0, width=36, height=64 }, { x=40, y=0, width=36, height=64 }, { x=80, y=0, width=36, height=64 }, { x=124, y=0, width=36, height=64 }, { x=168, y=0, width=64, height=64 } }, sheetContentWidth = 232, sheetContentHeight = 64 } local sliderSheet = graphics.newImageSheet( "sliderSheet.png", options ) -- Create the widget local slider = widget.newSlider { sheet = sliderSheet, leftFrame = 1, middleFrame = 2, rightFrame = 3, fillFrame = 4, frameWidth = 36, frameHeight = 64, handleFrame = 5, handleWidth = 64, handleHeight = 64, top = 200, left= 50, orientation = "horizontal", width = 300, listener = sliderListener }

what is all the  x=0, x=40 inside the – frames table

is it kind of like the sprites?

and by the way — how do I remove the slider

I try this in --destroy.scene

&nbsp;&nbsp;&nbsp; if slider then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;slider.removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;slider=nil &nbsp;&nbsp;&nbsp; end

it did not work

In the listener function you get passed a table called event.  One of the members of that table is called value:

event.value

This is a number from 0 to 100 that represents where the slider is.  You would do something like:

local function sliderListener( event ) &nbsp;&nbsp;&nbsp; print( "Slider at " .. event.value .. "%" ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if event.value \> 55 and event.value \< 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(soundBig) &nbsp;&nbsp;&nbsp; elseif event.value \>= 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;audio.play(soundSmall) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; &nbsp; end

Thanks Rob… That was Easy…

it WORKS!!!


what about the IMAGE or CUSTOM slider

local widget = require( "widget" ) -- Slider listener local function sliderListener( event ) print( "Slider at " .. event.value .. "%" ) end -- Image sheet options and declaration local options = { frames = { { x=0, y=0, width=36, height=64 }, { x=40, y=0, width=36, height=64 }, { x=80, y=0, width=36, height=64 }, { x=124, y=0, width=36, height=64 }, { x=168, y=0, width=64, height=64 } }, sheetContentWidth = 232, sheetContentHeight = 64 } local sliderSheet = graphics.newImageSheet( "sliderSheet.png", options ) -- Create the widget local slider = widget.newSlider { sheet = sliderSheet, leftFrame = 1, middleFrame = 2, rightFrame = 3, fillFrame = 4, frameWidth = 36, frameHeight = 64, handleFrame = 5, handleWidth = 64, handleHeight = 64, top = 200, left= 50, orientation = "horizontal", width = 300, listener = sliderListener }

what is all the  x=0, x=40 inside the – frames table

is it kind of like the sprites?

and by the way — how do I remove the slider

I try this in --destroy.scene

&nbsp;&nbsp;&nbsp; if slider then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;slider.removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;slider=nil &nbsp;&nbsp;&nbsp; end

it did not work