User Input help needed

Hello,

I’m trying to figure out the userInput feature of Corona and am starting by trying to build a very simple app that does this:

1 - Display text field
2 - Type in something and hit enter
3 - Entered text shows up on the screen.

I’m not having luck with this code (below). I get the following error:

WARNING: Attempting to set property(text) with nil

[code]
local text = display.newText("",250,150,nil,26)

local function textListener( event )

–print( event.text )

if event.phase == “submitted” then

text.text = event.text

end

end

– Create our Text Field
defaultField = native.newTextField( 10, 30, 180, 30 )
defaultField:addEventListener( “userInput”, textListener )
[/code] [import]uid: 141560 topic_id: 33655 reply_id: 333655[/import]

Hello,
This could be line #1 in fact, where you try to pass nil instead of a valid font name. Either supply a common font name, or put in native.systemFont to use the default on any device.

If this doesn’t work, then let me know and I’ll think about what else it might be.

Best regards,
Brent [import]uid: 200026 topic_id: 33655 reply_id: 133804[/import]

Thank you for your reply…

I tried changing that field to native.systemFont and still no luck

I you comment out the following

–print( event.text )

– if event.phase == “submitted” then

text.text = event.text

– end

it will display and erase text as you type and delete. but if you hit “enter” the text disappears - which must mean upon submitting passes a nil value instead of the entered text.

Hm… [import]uid: 141560 topic_id: 33655 reply_id: 133806[/import]

Hello again,
This is a bit odd, I agree! But, I think I solved it, using different phase responses:

local defaultField  
local text = display.newText("",250,350,native.systemFont,26) ; text:setTextColor(255,0,0)  
  
local function textListener( event )  
  
 if ( event.phase == "editing" ) then  
 text.text = event.text  
 elseif ( event.phase == "ended" or event.phase == "submitted" ) then   
 print( text.text )  
 end  
end  
  
-- Create our Text Field  
defaultField = native.newTextField( 10, 30, 180, 30 )  
defaultField:addEventListener( "userInput", textListener )  

It’s basically the same as you had, it just updates the text during the “editing” phase. Then, it doesn’t really do anything during the “submitted” phase except to look up the current text by the “.text” string of the text object. I think this will work for you, or do you need more functionality?

Brent
[import]uid: 200026 topic_id: 33655 reply_id: 133908[/import]

Thanks! That seems to do the trick… I modified your code slightly to display text after submitting.

I changed line 7 from text.text = event.text to

submittedtext = event.text

Also added this line after the elseif statement

text.text = submittedtext

and it worked

[import]uid: 141560 topic_id: 33655 reply_id: 133915[/import]

Hello,
This could be line #1 in fact, where you try to pass nil instead of a valid font name. Either supply a common font name, or put in native.systemFont to use the default on any device.

If this doesn’t work, then let me know and I’ll think about what else it might be.

Best regards,
Brent [import]uid: 200026 topic_id: 33655 reply_id: 133804[/import]

Thank you for your reply…

I tried changing that field to native.systemFont and still no luck

I you comment out the following

–print( event.text )

– if event.phase == “submitted” then

text.text = event.text

– end

it will display and erase text as you type and delete. but if you hit “enter” the text disappears - which must mean upon submitting passes a nil value instead of the entered text.

Hm… [import]uid: 141560 topic_id: 33655 reply_id: 133806[/import]

Hello again,
This is a bit odd, I agree! But, I think I solved it, using different phase responses:

local defaultField  
local text = display.newText("",250,350,native.systemFont,26) ; text:setTextColor(255,0,0)  
  
local function textListener( event )  
  
 if ( event.phase == "editing" ) then  
 text.text = event.text  
 elseif ( event.phase == "ended" or event.phase == "submitted" ) then   
 print( text.text )  
 end  
end  
  
-- Create our Text Field  
defaultField = native.newTextField( 10, 30, 180, 30 )  
defaultField:addEventListener( "userInput", textListener )  

It’s basically the same as you had, it just updates the text during the “editing” phase. Then, it doesn’t really do anything during the “submitted” phase except to look up the current text by the “.text” string of the text object. I think this will work for you, or do you need more functionality?

Brent
[import]uid: 200026 topic_id: 33655 reply_id: 133908[/import]

Thanks! That seems to do the trick… I modified your code slightly to display text after submitting.

I changed line 7 from text.text = event.text to

submittedtext = event.text

Also added this line after the elseif statement

text.text = submittedtext

and it worked

[import]uid: 141560 topic_id: 33655 reply_id: 133915[/import]