Hello, I need to write code that displays two sliders and then adds their values and displays the value.
The code to show the two sliders and their individual values works great, but i can’t figure out how to share their value in order to add the two values.
Here is my code.
Everything works but the sum function. Thank you in advance.
local widget = require “widget”
cx = display.contentCenterX
cy = display.contentCenterY
cw = display.contentWidth
ch = display.contentHeight
f = native.systemFont
local valueOne
local valueTwo
–function to set value between 1-100 this works fine
local function getValue(value)
slide_value= math.floor(100*value/100)
return slide_value
end
–set value of slider one
local function getValueOne(value)
valueOne=value
end
–return value of slider one
local function returnValue()
return valueOne
end
--set value of slider two
local function getValueTwo(value)
valueTwo=value
end
local function returnValueTwo()
return valueTwo
end
–Slider One function
local function slideHandler(event)
--modifies value from 1-100
slideOne = getValue(event.value)
txt_slideOne.text = slideOne
end
–Slider Two function
local function slideHandlerTwo(event)
if event.phase ~= previousPhase then
previousPhase = event.phase
end
slideTwo = getValue(event.value)
txt_slideTwo.text = slideTwo
end
–My attemo to sum both functions it does not work
local function sum()
valuetwo=returnValueTwo()
valueone=returnValue()
sum=valueone+valuetwo
txt_number.text = sum
end
–slideOne
sliderHandler = widget.newSlider
{
x = cx,
y = cy+50,
width = .9*cw,
listener = slideHandler
}
–displaycurrent value
txt_slideOne = display.newText(sliderHandler.value,cx+20,cy+30,“century gothic”,16)
–slide two
sliderHandlerTwo = widget.newSlider
{
x = cx,
y = cy+100,
width = .9*cw,
listener = slideHandlerTwo
}
txt_slideTwo = display.newText(sliderHandlerTwo.value,cx+20,cy+80,“century gothic”,16)
–it is suppose to diplay the sum
txt_number = display.newText(“0”, cx, 0.25*ch,“century gothic”, 200)