Sorry I should have commented the code a little better
Here is a modification that allows for single digit answers and double digit answers, there are a million different ways you can accomplish this via code and this method is very rigid and really is only meant to exhibit the requested behavior, it could be done a lot better:
[code]
local buttonVars = {
{text = “0”, id = 0, x = 50, y = 50},
{text = “1”, id = 1, x = 100, y = 50},
{text = “2”, id = 2, x = 150, y = 50},
{text = “3”, id = 3, x = 200, y = 50},
{text = “4”, id = 4, x = 250, y = 50},
{text = “5”, id = 5, x = 300, y = 50},
{text = “6”, id = 6, x = 350, y = 50},
{text = “7”, id = 7, x = 400, y = 50},
{text = “8”, id = 8, x = 50, y = 100},
{text = “9”, id = 9, x = 100, y = 100},
}
local buttons = {}
local question = display.newText(“2 + 2 equals?”, display.contentCenterX, display.contentCenterY, nil, 24)
local answerBox = display.newRect(300, 200, 100, 50)
answerBox.text1 = display.newText("", answerBox.x - 30, answerBox.y - 18, nil, 24)
answerBox.text1:setTextColor(0, 0, 0)
answerBox.text2 = display.newText("", answerBox.x , answerBox.y - 18, nil, 24)
answerBox.text2:setTextColor(0, 0, 0)
local answerToggle = 0
local results
local enteredAnswer = “”
local correctAnswer = 10
local function checkResults()
–Check answer
if string.len(answerBox.text2.text) > 0 then --If the second text box string is not more than 0 then we know only a single digit was entered
enteredAnswer = answerBox.text1.text … answerBox.text2.text
else
enteredAnswer = answerBox.text1.text
end
if enteredAnswer == tostring(correctAnswer) then
results.text = “Result : Correct!”
else
results.text = “Result : Incorrect!”
end
–Reset the text and toggle
answerBox.text1.text = “”
answerBox.text2.text = “”
answerToggle = 0
return true
end
results = display.newText(“Check answer”, 100, 280, nil, 24)
results:addEventListener(“tap”, checkResults)
local function handleButtons(event)
local phase = event.phase
local buttonTouched = event.target
local buttonTouchedID = event.target.id
answerToggle = 1 - answerToggle
if answerToggle == 1 then
answerBox.text1.text = buttonTouched.text.text
else
answerBox.text2.text = buttonTouched.text.text
end
results.text = “Check answer”
return true
end
–set the buttons values
for i = 1, #buttonVars do
buttons[i] = display.newRect(buttonVars[i].x, buttonVars[i].y, 40, 40)
buttons[i].text = display.newText(buttonVars[i].text, buttonVars[i].x + 10, buttonVars[i].y + 4, nil, 24)
buttons[i].text:setTextColor(0, 0, 0)
buttons[i]:addEventListener(“tap”, handleButtons)
end
[/code] [import]uid: 84637 topic_id: 26435 reply_id: 107522[/import]