rob:
Before I take a crack at answering your question, I want to point out something. See all those “end” statements all lined up nice and neat? Well it makes your code very… very… hard to read. Each end needs to be indented to the same level as the statement that needs ended. For instance:
local function doSomething() if someCondition then -- do stuff end end
See how the ends match up with the statement they are ending? Please see this tutorial: https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/
Now here is your code properly formatted:
local function onNickEditing( event ) if event.phase == "began" then if event.target.text == "Nickname..." then event.target.text = "" if event.phase == "editing" then if (string.len(event.target.text) \> 8) then event.target.text = event.target.text:sub(1, 8) if event.phase == "ended" then if (string.len(event.text) \< 4) then event.target.text = "Nickname..." end end end end end end end
Once you see the code properly formatted, you should be able to trace through it. Your first if statement tests the event phase == “began” Inside that if block you test event.phase again to see the phase is editing. But that condition will never be true because you’re in the middle of a conditions where phase is “began”.
This is likely the code you are looking for:
local function onNickEditing( event ) if event.phase == "began" then if event.target.text == "Nickname..." then event.target.text = "" elseif event.phase == "editing" then if (string.len(event.target.text) \> 8) then event.target.text = event.target.text:sub(1, 8) end elseif event.phase == "ended" then if (string.len(event.text) \< 4) then event.target.text = "Nickname..." end end end end
That should get you on track, but you can simplify this even more. It looks like “Nickname…” is intended to be what’s called a Placeholder, which is text that goes away once the user starts typing in the field. If you do:
usernameField.placeholder = “Nickname…”
The field will show that text for you and automatically show it until there are typed characters. I think this does what you want:
local function onNickEditing( event ) if event.phase == "editing" then if (string.len(event.target.text) \> 8) then event.target.text = event.target.text:sub(1, 8) end elseif event.phase == "ended" then if (string.len(event.text) \< 4) then event.target.text = "" end end end
Rob
I would never imagine an answer so detailed like yours. Let me start saying a big thank you for all the tips and knowledge shown in your post.
I loved to see that placeholder feature but it gets in the way in my alpha interpolation from 0 to 0.65. ( it doesn’t disappear when alpha = 0 ). With your post, I was able to do everything I was imagining and went beyond. check this out Thank your for being part of my learning journey !
local function onNickEditing( event ) if event.phase == "began" then if event.target.text == "Nickname..." then event.target.text = "" end if event.target.text ~= "Nickname..." then event.target.text = event.target.text end elseif event.phase == "editing" then if (string.len(event.target.text) \> 8) then event.target.text = event.target.text:sub(1, 8) end elseif event.phase == "ended" then if (string.len(event.target.text) \< 4) then event.target.text = "Nickname..." end end end usernameField = native.newTextField( centerX, centerY - 14, 150, 36) usernameField.font = native.newFont( "Gotham", 25 ) usernameField.text = "Nickname..." --usernameField.placeholder = "Nickname..." -- didn't use but it's good for future reference! usernameField:setTextColor( 0, 0, 0, 0) usernameField.inputType = "default" usernameField.hasBackground = false usernameField.isEditable = true usernameField:addEventListener( "userInput", onNickEditing ) local function showNickAlpha () usernameField:setTextColor( 0, 0, 0, 0.65 ) end timer.performWithDelay( 3700, showNickAlpha )