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