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
print("screenfade")
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
local function checkAnswer()
-- check submitted answer
end
local function textListener( event )
if ( event.phase == "began" ) then
-- User begins editing "numericField"
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
checkAnswer()
end
end
-- Create text field
numericField = native.newTextField( 150, 150, 180, 30 )
numericField.inputType = "number"
numericField:addEventListener( "userInput", textListener )
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.
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()
-- check submitted answer;
-- if the answer is equal to the number of bats that entered the cave, then the answer is correct
if (answer == purpleBatCount ) then
print("Correct Answer")
if (answer == greenBatCount ) then
print("Correct Answer")
if (answer == blueBatCount ) then
print("Correct Answer")
if (answer == totalBatCount ) then
print("Correct Answer")
else print("Wrong Answer")
purpleBatCount = tonumber(purpleBatCount)
greenBatCount = tonumber(greenBatCount)
blueBatCount = tonumber(blueBatCount)
totalBatCount = tonumber(totalBatCount)
end
end
checkAnswer()
local function textListener( event )
if (event.phase == "began" ) then
print ("began")
elseif (event.phase == "ended" or
event.phase == "submitted") then
print("final input")
checkAnswer()
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
local function checkAnswer(valueSubmitted)
if (valueSubmitted == purpleBatCount) then
print "purple bat"
elseif (valueSubmitted == blueBatCount) then
print "blue bat"
else
print "wrong answer"
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 )
checkAnswer(valueTextField)
end
end
-- Create text field
numericField = native.newTextField( 150, 150, 180, 100 )
numericField.inputType = "number"
numericField:addEventListener( "userInput", textListener )
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 )
numericField:addEventListener("userInput" , textListener )
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.
I presume your checkAnswer function looks something like this:
local function checkAnswer(valueSubmitted)
if (valueSubmitted == purpleBatCount) then
print "Correct Answer"
elseif (valueSubmitted == blueBatCount) then
print "Correct Answer"
else
print "wrong answer"
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 correctAnswer
In your checkAnswer function, compare valueSubmitted to correctAnswer like this:
local function checkAnswer(valueSubmitted)
if (valueSubmitted == correctAnswer) then
print ("correct answer")
else
print ("wrong answer")
end
end
Then, when you are picking the colour, set correctAnswer as appropriate:
if randomImage == 1 then
correctAnswer = purpleBatCount
elseif randomImage == 2 then
correctAnswer = greenBatCount
...
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.
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:
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
print("Correct Answer")
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.