Hi all,
Just got back into Corona and going over my old programs. I am trying to cut down the repetitiveness of my code by using a function to create widgets rather than creating them individually. This is working except where I need to update the label of a button and I get an error about trying to index either a global or upvalue. I know the problem is something to do with scope but I can’t find it.
Thanks in advance for any help.
local warriorBuy
local function createBtn ( name, btnLabel, eventCall, btnWidth, btnHeight, xLocation, yLocation )
– Create a button that will call a function
name = widget.newButton(
{
label = btnLabel,
onEvent = eventCall,
emboss = false,
shape = “roundedRect”,
width = btnWidth,
height = btnHeight,
cornerRadius = 2,
fillColor = { default={0,0,0,1}, over={0,0,0,1} },
strokeColor = {default={1,1,1,1}, over={1,1,1,1} },
strokeWidth = 4,
}
)
sceneGroup:insert(name)
name.x = xLocation
name.y = yLocation
end
local function increaseWarrior ( event )
– Let player buy a warrior for the said price, given they have enough money
if ( “ended” == event.phase ) then
if mydata.money >= mydata.warriorBuy then
mydata.warriorAm = mydata.warriorAm + 1
mydata.money = mydata.money - mydata.warriorBuy
mydata.warriorBuy = math.floor( (mydata.warriorAm)^(1.01) )
moneyHave.text = "Money "…mydata.money
unitWarriors.text = “Warriors:”…mydata.warriorAm
buyWarrior:setLabel(“Buy Warrior $”…mydata.warriorBuy) --This is where the error occurs
if mydata.warriorAm >= mydata.warriorMax then
mydata.warriorMax = mydata.warriorAm
end
end
end
end
createBtn( buyWarrior, “Buy Warrior $”…mydata.warriorBuy, increaseWarrior, 150, 50, cenx, (toty*4)/20 )