Very simple calculation method

Hi guys,

I 'm trying to make a simple calculation, I have created two text fields both use number input. What I am trying to do is when the user enters and input into both text fields at new text and will present what both of them multiplied together is. How would this look this is what I have but it doesn’t work at all…

[lua]

–textResults

local textResults= display.newText(“0”, 

172, 180, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )

textResults:setFillColor( 0, 0, 0 )

–textboxVaule1

local textboxVaule1 = native.newTextField( 50, 120, 75, 40 )

textboxVaule1.size = 20

textboxVaule1:addEventListener( “userInput”, textboxVaule1 )

–textboxVaule2

local textboxVaule2 = native.newTextField( 50, 120, 75, 40 )

textboxVaule2.size = 20

textboxVaule2:addEventListener( “userInput”, textboxVaule2 )

local function btnNext ( event )

textboxValue1.text * textboxValue2.text = textResult.text

end

–btnNext

local btnNext = widget.newButton

{

   width = 150,

   height = 40,

   defaultFile = “btnNext.png”,

   onEvent = btnNext

}

btnNext.x = 235

btnNext.y = 445

[/lua]

Thanks again,

Matt.

[lua]

textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text)

[/lua]

It appears your text fields are being created right on top of each other as well. 

I have now moved the second text field down 100px forgot to change that I have tested the 

[lua]

textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text

[/lua]

and the result was a runtime error saying

Runtime error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textbocxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

Any ideas

Thanks again,

Matt.

attempt to index global ‘textbocxValue1’ (a nil value)

Still getting the issue…

Runtime error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textboxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

 

Thanks,

Matt.

also, textboxV aul e1 instead of textboxV alu e1.

Same for 2.

Also, does your btnNext function not give you an error? The contents seem to be the wrong way around.

  1. I have rewrote it and am still getting the same Runtime Error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textboxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

  1. [lua]
  2. –textResults
  3. local textResults= display.newText(“0”, 
  4. 172, 180, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )
  5. textResults:setFillColor( 0, 0, 0 )
  6.  
  7. –textboxValue1
  8. local textboxValue1= native.newTextField( 50, 120, 75, 40 )
  9. textboxValue1.size = 20
  10. textboxValue1:addEventListener( “userInput”, textboxValue1)
  11. textboxValue1.inputType = “number”
  12.  
  13. –textboxValue2
  14. local textboxValue2= native.newTextField( 50, 120, 75, 40 )
  15. textboxValue2.size = 20
  16. textboxValue2:addEventListener( “userInput”, textboxValue2)
  17. textboxValue2.inputType = “number”
  18.  
  19. local function btnNext ( event )
  20. textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text)
  21. end   
  22. –btnNext
  23. local btnNext = widget.newButton
  24. {
  25.    width = 150,
  26.    height = 40,
  27.    defaultFile = “btnNext.png”,
  28.    onEvent = btnNext
  29. }
  30.  
  31. btnNext.x = 235
  32. btnNext.y = 445
  33. [/lua]
  34. Thanks again,
  35. Matt.

Try this:

local textResult= display.newText("0", 172, 180, display.contentWidth\*0.1, display.contentHeight \* 0.1, native.systemFont, 13 ) textResult:setFillColor( 1, 0, 0 ) local function textListener( event ) end --textboxValue1 local textboxValue1= native.newTextField( 50, 120, 75, 40 ) textboxValue1.size = 20 textboxValue1:addEventListener( "userInput", textListener) textboxValue1.inputType = "number" textboxValue1.text = "1" --textboxValue2 local textboxValue2= native.newTextField( 50, 220, 75, 40 ) textboxValue2.size = 20 textboxValue2:addEventListener( "userInput", textListener) textboxValue2.inputType = "number" textboxValue2.text = "2" local function btnNextHandler ( event ) textResult.text = tonumber(textboxValue1.text) \* tonumber(textboxValue2.text) end btnNext = widget.newButton{ label="Rate", labelColor = { default={255}, over={128} }, defaultFile="btnover.png", overFile="btn.png", width=154, height=40, onRelease = btnNextHandler -- event listener function } btnNext.x = display.contentWidth\*0.4 btnNext.y = display.contentHeight\*0.4

This has worked perfectly for what I needed, is there a method that I can use where the a text field input is empty and if so then calls an event which shows an alert on screen like this…

[lua]

local function onComplete( event )

   if event.action == “clicked” then

        local i = event.index

        if i == 1 then

        end

    end

end

[/lua]

With my button function like this…

[lua]

local function btnNextHandler( event )

textboxResults.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text)

if 

textboxValue1.text == “” then

local alert = native.showAlert( “Warning”, “ERROR: No input in textbox(s)”, { “OK” }, onComplete)

end

[/lua]

But I keep getting an error saying 

Runtime error

error loading module ‘Page10.lua’ from file ‘c:\users\matt\documents\corona projects\hs app\Page10.lua’:

c:\users\matt\documents\corona projects\hs app\Page9.lua:195: ‘end’ expected (to close ‘function’ at line 26) near ‘<eof>’

stack traceback:

[C]: in function ‘error’

?: in function ‘gotoScene’

c:\users\matt\documents\corona projects\hs app\Page1.lua:12: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221>

Thanks again,

Matt.

I haven’t tested it but I think you just need a small adjustment to your button handler.

local function btnNextHandler ( event ) if textboxValue1.text == nil or textboxValue1.text == "" or textboxValue2.text == nil or textboxValue2.text == "" then local alert = native.showAlert( "Warning", "ERROR: No input in textbox(s)", { "OK" }, onComplete ) else textResult.text = tonumber(textboxValue1.text) \* tonumber(textboxValue2.text) end end

This worked perfectly.

Thanks,

Matt.

[lua]

textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text)

[/lua]

It appears your text fields are being created right on top of each other as well. 

I have now moved the second text field down 100px forgot to change that I have tested the 

[lua]

textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text

[/lua]

and the result was a runtime error saying

Runtime error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textbocxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

Any ideas

Thanks again,

Matt.

attempt to index global ‘textbocxValue1’ (a nil value)

Still getting the issue…

Runtime error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textboxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

 

Thanks,

Matt.

also, textboxV aul e1 instead of textboxV alu e1.

Same for 2.

Also, does your btnNext function not give you an error? The contents seem to be the wrong way around.

  1. I have rewrote it and am still getting the same Runtime Error

c:\users\matt\documents\corona projects\hs app\page1.lua:17: attempt to index global ‘textboxValue1’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app\page1.lua:17: in function ‘_onEvent’

?: in function ‘?’

?: in function <?:677>

?: in function <?:221> 

  1. [lua]
  2. –textResults
  3. local textResults= display.newText(“0”, 
  4. 172, 180, display.contentWidth, display.contentHeight * 0.5, native.systemFont, 13 )
  5. textResults:setFillColor( 0, 0, 0 )
  6.  
  7. –textboxValue1
  8. local textboxValue1= native.newTextField( 50, 120, 75, 40 )
  9. textboxValue1.size = 20
  10. textboxValue1:addEventListener( “userInput”, textboxValue1)
  11. textboxValue1.inputType = “number”
  12.  
  13. –textboxValue2
  14. local textboxValue2= native.newTextField( 50, 120, 75, 40 )
  15. textboxValue2.size = 20
  16. textboxValue2:addEventListener( “userInput”, textboxValue2)
  17. textboxValue2.inputType = “number”
  18.  
  19. local function btnNext ( event )
  20. textResult.text = tonumber(textboxValue1.text) * tonumber(textboxValue2.text)
  21. end   
  22. –btnNext
  23. local btnNext = widget.newButton
  24. {
  25.    width = 150,
  26.    height = 40,
  27.    defaultFile = “btnNext.png”,
  28.    onEvent = btnNext
  29. }
  30.  
  31. btnNext.x = 235
  32. btnNext.y = 445
  33. [/lua]
  34. Thanks again,
  35. Matt.

Try this:

local textResult= display.newText("0", 172, 180, display.contentWidth\*0.1, display.contentHeight \* 0.1, native.systemFont, 13 ) textResult:setFillColor( 1, 0, 0 ) local function textListener( event ) end --textboxValue1 local textboxValue1= native.newTextField( 50, 120, 75, 40 ) textboxValue1.size = 20 textboxValue1:addEventListener( "userInput", textListener) textboxValue1.inputType = "number" textboxValue1.text = "1" --textboxValue2 local textboxValue2= native.newTextField( 50, 220, 75, 40 ) textboxValue2.size = 20 textboxValue2:addEventListener( "userInput", textListener) textboxValue2.inputType = "number" textboxValue2.text = "2" local function btnNextHandler ( event ) textResult.text = tonumber(textboxValue1.text) \* tonumber(textboxValue2.text) end btnNext = widget.newButton{ label="Rate", labelColor = { default={255}, over={128} }, defaultFile="btnover.png", overFile="btn.png", width=154, height=40, onRelease = btnNextHandler -- event listener function } btnNext.x = display.contentWidth\*0.4 btnNext.y = display.contentHeight\*0.4