# Using numericField

Hi, I’m trying to create a game where the player has to correctly guess the number of bats that have entered a cave.
I created the variables ‘purplebatCount’ and ‘greenbatCount’ so they would increase by 1 when a bat of that colour (purple or green in this case) entered the cave. How would I use ‘numericField’ so the player can input the correct number?

``````function timerDown(event)
timeLimit = timeLimit - 1

if timeLimit== 2 then

transition.to( background, { alpha=0.5  } )
transition.to( cave, { alpha=0  } )
end
if timeLimit == 0 then --When the time reaches 0, a random question will appear
randomImage = math.random(1,4)
end
local purplebatCount -- Number of purple bats that entered the cave
local greenbatCount -- Number of green bats that entered the cave
-- How many purple bats entered the cave?
if randomImage == 1 then
print("purpleMessage")
purpleMessage = display.newImageRect( mainGroup, "purpleMessagep.png",  500, 500)
purpleMessage.x = display.contentCenterX
purpleMessage.y = display.contentHeight - 200
numericField = native.newTextField( 155, 255, 90, 30 ) -- Number text box
numericField.inputType = "number"
end
--How many green bats entered the cave?
if randomImage == 2 then
print("greenMessage")
greenMessage = display.newImageRect( mainGroup, "greenMessageg.png",  500, 500)
greenMessage.x = display.contentCenterX
greenMessage.y = display.contentHeight - 200
numericField = native.newTextField( 155, 255, 90, 30 ) -- Number text box
numericField.inputType = "number"
end
``````

I’m relatively new to using Lua so you’ll need to explain this to me in a way I’ll understand if that’s ok.
Thanks

Expanding the sample code that can be found on documentation to fit your needs. This should do the trick.

``````local numericField

end

local function textListener( event )

if ( event.phase == "began" ) then
-- User begins editing "numericField"
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
end
end

-- Create text field
numericField = native.newTextField( 150, 150, 180, 30 )
numericField.inputType = "number"

Also, do remember that the values of the the textfield is going to be a string, so you’ll need to change it to number via tonumber before comparing it to numeric values.

1 Like

Hi, sorry for the (really) late reply, didn’t have much time to work on the code over the past 2 weeks .
How would I go about changing the values to tonumber?
This is what I have done so far but it doesn’t seem to work.

``````local function checkAnswer()
-- if the answer is equal to the number of bats that entered the cave, then the answer is correct
if (answer == purpleBatCount ) then
if (answer == greenBatCount ) then
if (answer == blueBatCount ) then
if (answer == totalBatCount ) then
purpleBatCount = tonumber(purpleBatCount)
greenBatCount = tonumber(greenBatCount)
blueBatCount = tonumber(blueBatCount)
totalBatCount = tonumber(totalBatCount)
end
end

local function textListener( event )
if (event.phase == "began" ) then
print ("began")
elseif (event.phase == "ended" or
event.phase == "submitted") then
print("final input")
end
end
``````

The problem is that when entering a value, the print comes out as “Wrong Answer” every time.

I’m sorry. It seems that I forgot that you are a beginner when sharing the sample code above. What @XeduR says is that you need to convert what’s entered into the text field into number format with `tonumber()`. Something like this:

``````local numericField

local purpleBatCount = 5
local blueBatCount = 10

if (valueSubmitted == purpleBatCount) then
print "purple bat"
elseif (valueSubmitted == blueBatCount) then
print "blue bat"
else
end
end

local function textListener( event )

if ( event.phase == "began" ) then
-- User begins editing "numericField"
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
local valueTextField = tonumber( event.target.text )
end
end

-- Create text field
numericField = native.newTextField( 150, 150, 180, 100 )
numericField.inputType = "number"

So the code works, except that I keep getting the correct answer regardless of the question asked, for example:

``````    --How many green bats entered the cave?----------
if randomImage == 2 then -- One of four possible questions
greenMessage = display.newImageRect(mainGroup, "greenMessageg.png", 500, 500)
greenMessage.x = display.contentCenterX
greenMessage.y = display.contentHeight  200

numericField.inputType = "number"
numericField.font = native.newfont(native.systemFontBold, 18 )
``````

If I typed in the correct value for ‘blueBatCount’ instead of ‘greenBatCount’, it would still print “Correct Answer” even though the question is asking for the number of green bats.

``````local function checkAnswer(valueSubmitted)
if (valueSubmitted == purpleBatCount) then
elseif (valueSubmitted == blueBatCount) then
else
end
end
``````

Then it is behaving as expected. It prints “Correct Answer” if the valueSubmitted == purpleBatCount, elseif valueSubmitted == blueBatCount then it prints “Correct Answer” in that case also. You need to consider which value it is the player needs to guess. Try forward-declaring a variable called correct answer, like this:

``````local purpleBatCount = 5
local blueBatCount = 10
``````

``````local function checkAnswer(valueSubmitted)
else
end
end
``````

Then, when you are picking the colour, set correctAnswer as appropriate:

``````if randomImage == 1 then
elseif randomImage == 2 then
...``````

I got an error saying ‘=’ expected near ‘==’ so I removed an ‘=’ sign when setting correctAnswer so it looks like:

``````  correctAnswer = purpleBatCount
``````

The code worked after that but when I entered the correct value for purpleBatCount (and the other colours), I got an error saying; attempt to call local ‘valueSubmitted’ ( a number value)
Not sure why that is

Oops! That of course should have been = and not ==. I’ve edited my post.

It sounds like you are calling valueSubmitted as a function:

`valueSubmitted()`

This won’t work as it is a number value (as the error says). Hard to give further advice without seeing the code for your checkAnswer function. Do post it.

Ohh I see what happened, I put valueSubmitted(“Correct answer”) instead of print(“Correct answer”) and didn’t notice somehow

The code works now, thanks a bunch , you too @bgmadclown and @XeduR.

I also wanted to add a message saying ‘Correct!’ after typing in the correct value. This is what I had:

``````                  if (valueSubmitted == correctAnswer) then
correct = display.newImageRect(mainGroup, "correct.png", 500, 500)
correct.x = display.contentCenterX
correct.y = display.contentHeight - 200
``````

Though the message only shows up for about a second…

I can’t tell from that why the message disappears. Are you deleting it somewhere else in your code, or moving it somewhere else?

I assume the player is asked multiple questions and that the message will show up multiple times? In that case, you shouldn’t be creating a new image every time. Just make it once outside of (and above) any functions and make it invisible:

``````local correctMessage = display.newImage...
correctMessage.isVisible = false
``````

Then when the player gets it right just make it visible and use a timer to make it in visible when you want:

``````correctMessage.isVisible = true
timer.performWithDelay(5000, function() correctMessage.isVisible = false end)``````

Hi again sorry for the late reply,
The idea is that the question is asked once and then after the player gets it right they move onto the next level
I tried using ‘is.Visible’ but it didn’t seem to work, so I used ‘alpha’ instead:

``````            if (valueSubmitted == correctAnswer) then
correct = display.newImageRect(mainGroup, "correct.png", 500, 500)
correct.x = display.contentCenterX
correct.y = display.contentHeight - 200
transition.to( textBoxGroup, {alpha=0 } ) -- Makes the question invisible
transtition.to {correctGroup, {alpha=1, time=3000 } ) -- Makes the 'correct' message visible
``````

The message appeared afterwards, but wouldn’t go away even after I put ‘time =3000’
Also not sure how to remove the numeric textfield…

.isVisible should work.
I see nothing in what you’ve just posted that is problematic, the issue is elsewhere. If you don’t want to post it all here, you can pm me your project and I’ll take a look.

Okay, I pm’d the project to you in case you haven’t seen it