<RESOLVED>** New to CoronaSDK ** Graphics button and text question

I’m attempting to develop a simple math game just for multiplication. After reading through several tutorials and the documentation I’ve managed to generate 2 random numbers and store the answer in a string to be checked later one a button is clicked.

This is where I am stuck. I have graphics labeled 0-9 on screen and I want the user to click on the images and display the numbers in a text field (realtime) and once complete, click the check answer button and compare user input to answer. Is this possible? Thanks for any help!

Will [import]uid: 148860 topic_id: 26435 reply_id: 326435[/import]

Here is a working example I cooked up for you (its basic but gives you the logic to build on)
–main.lua

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("5 + 5 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 results = display.newText("Result :", 100, 280, nil, 24)  
local answerToggle = 0  
local enteredAnswer = ""  
local correctAnswer = 10  
  
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  
  
 --Check answer  
 enteredAnswer = answerBox.text1.text .. answerBox.text2.text  
  
 if enteredAnswer == tostring(correctAnswer) then  
 results.text = "Result : Correct!"  
 else  
 results.text = "Result : Incorrect!"  
 end  
 end  
  
 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  

–config.lua

application =  
{  
 content =  
 {  
 width = 320,  
 height = 480,  
 scale = "zoomEven",  
 fps = 60,  
 antialias = true  
 },  
}  

–build.settings

settings =  
{  
 orientation =  
 {  
 default = "landscapeRight",  
 supported =  
 {  
 "landscapeRight", "landscapeLeft"  
 },  
 },  
  
 iphone =  
 {  
 plist =  
 {  
 UIStatusBarHidden = true,  
 UIPrerenderedIcon="YES",  
 },  
 },  
}  

[import]uid: 84637 topic_id: 26435 reply_id: 107290[/import]

Thanks for the response. I’ll give it a try and see what I can do with it.

[import]uid: 148860 topic_id: 26435 reply_id: 107327[/import]

Danny,
Thanks again. I got it working. I’ve generated the random questions and have everything but I’ve run into a problem. If the answer is a single digit it doesn’t check the enteredAnswer

Also, because I’m not trying to look for a quick fix, I’m actually trying to grasp the concept of each line of code, I’ve added comments in the code that you’ve provided. Which leads me to another stump. I understand everything else I’m a bit confused when it comes to the handleButtons function and how it works as well as the answerToggle & enteredAnswer variables ANy help would be appreciated.

[code]
local answerToggle = 0
local enteredAnswer = “”
local correctAnswer = questionOne.text * questionTwo.text

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

–Check answer
enteredAnswer = answerBox.text1.text … answerBox.text2.text

if enteredAnswer == tostring(correctAnswer) then
results.text = “Result : Correct!”
else
results.text = “Result : Incorrect!”
end
end

return true
end
[/code] [import]uid: 148860 topic_id: 26435 reply_id: 107388[/import]

Ok so I’m figuring so things out slowly. I think I’m on the path to fixing my first problem. I’ve created a variable and used string.len to give the length of the answer. Now I’m working my way around adding that into the if statement. [import]uid: 148860 topic_id: 26435 reply_id: 107447[/import]

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]