I want to display a text with a number and to update the number when pressing a button. Here is what I got right now:
local oxyPar = 10
--the oxyPar is just a number
local oxyOpt =
{
text = "Oxygen: ".. tostring( oxyPar )
--all other text parameters
}
local oxygen = display.newText( oxyOpt )
--display a text calling oxyOpt table for the parameters
local timeOpt =
{
--all the button parameters
onRelease = timeOn
--call timeOn function on the button release
}
local timeBtn = widget.newButton( timeOpt )
--a button that calls timeOpt table for the parameters
local function timeOn( listener )
oxyPar = oxyPar + 1
end
After pressing the button the oxyPar (the number) should grow by one, but the text still shows up Oxygen: 10 instead of Oxygen: 11. Is there a way to update the text so it shows the new number?
changing
oxyPardoes not affect your display objectoxygen.A number value is copied by value so
is equivalent to
There is no relation between
oxyParandoxyOptas you simply copied the return value oftostring(10)intooxyOpt.textwhich is a different variable.Change
ogygen.textinsteadPlease refer to the Solar2d reference manual.
https://docs.coronalabs.com/api/library/display/newText.html