In onButtonEvent function t.value not updating

I am designing an Odd One Out round for a game that I am developing, every time a button is pressed, the onButtonEvent function is supposed to check if the word selected is the odd one out and then refreshes the buttons with different words. The words are updating and the value assigned to each button is updating, I have been printing it out to the console. I think the problem is when I assign right[num] = t.value the only values that go into to the table are the first 3 values that were originally assigned to the buttons. Why is this not being updated?

[code]local function onButtonEvent( event )
local t = event.target
–local phase = event.phase
if event.phase == “release” then
–num = num + 1
displayBtns()
right[num] = t.value
print(right[1])
print(right[2])
print(right[3])
print(right[4])
– --s3 = table.concat(right)
–print(s3)
if right[num] == answers[num].right then
print(“correct!”)
elseif right[num] == answers[num].wrong then
– t.alpha = 0
– select.alpha = 1
print(“incorrect!”)
elseif right[num] == answers[num].wrong2 then
else
print(“incorrect!”)
end
num = num + 1
num2 = num2 + 1
s3 = “”
– display.remove( btn3 )
– btn3 = nil
– display.remove( btn2 )
– btn2 = nil
– display.remove( btn1 )
– btn1 = nil
end
–num = math.random(1,#t)
s3 = “”
end

function displayBtns()
btn3 = widget.newButton{
default = “images/Round4_blue_button.png”,
label = answers[num].right,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}

btn3.value = answers[num].right
print(btn3.value)

btn2 = widget.newButton{
default = “images/Round4_blue_button.png”,
label = answers[num].wrong2,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn2.value = answers[num].wrong2
print(btn2.value)

btn1 = widget.newButton{
default = “images/Round4_blue_button.png”,
label = answers[num].wrong,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn1.value = answers[num].wrong
print(btn1.value)
print("----------")

–btn3.label = answers[num].right
– p1Button = display.newImage(“images/SinglePlayer_button.png”, 90, 140)
– p2Button = display.newImage(“images/2Player_button.png”, 90, 220)

– p1Button.touch = onSceneTouch
– p1Button:addEventListener(“touch”, p1Button)

– p2Button.touch = onSceneTouch1
– p2Button:addEventListener(“touch”, p2Button)
btn1.x = 90; btn1.y = 245
btn2.x = 240; btn2.y = 245
btn3.x = 390; btn3.y = 245
– p2Button.x = 240; p2Button.y = 260

end
displayBtns()
[/code] [import]uid: 208811 topic_id: 35070 reply_id: 335070[/import]

I’m not very comfortable with how you’re creating the buttons over and over. It seems like its going to leak memory badly since you are creating new widgets each time over top of the existing widgets. You’re creating event handlers inside event handlers to handle the same event. It feels unnecessarily recursive.

What I would recommend is create the buttons once and inside your button handler when you want to change the label of the button, use the button’s :setLabel() method to change the text on the existing button. See:

http://docs.coronalabs.com/api/type/ButtonWidget/setLabel.html

for more info. [import]uid: 199310 topic_id: 35070 reply_id: 139469[/import]

Thanks Rob, I took your advice and stopped creating new widgets on top of existing widgets, I’m still fairly new to this, but that is no excuse. I am now changing the labels of the widgets in the event handler. I still cant figure out why the only values that go into to the right[num] table are the first 3 values that were originally assigned to the buttons. Why is this not being updated? [import]uid: 208811 topic_id: 35070 reply_id: 139471[/import]

I’m not very comfortable with how you’re creating the buttons over and over. It seems like its going to leak memory badly since you are creating new widgets each time over top of the existing widgets. You’re creating event handlers inside event handlers to handle the same event. It feels unnecessarily recursive.

What I would recommend is create the buttons once and inside your button handler when you want to change the label of the button, use the button’s :setLabel() method to change the text on the existing button. See:

http://docs.coronalabs.com/api/type/ButtonWidget/setLabel.html

for more info. [import]uid: 199310 topic_id: 35070 reply_id: 139469[/import]

Thanks Rob, I took your advice and stopped creating new widgets on top of existing widgets, I’m still fairly new to this, but that is no excuse. I am now changing the labels of the widgets in the event handler. I still cant figure out why the only values that go into to the right[num] table are the first 3 values that were originally assigned to the buttons. Why is this not being updated? [import]uid: 208811 topic_id: 35070 reply_id: 139471[/import]