Roger that. Thanks for answering. I really do appreciate that and it helps me a lot.
Now, let me help a little with the original question, and then give some advice that should help you ramp up.
Original Question
I grabbed the first code block from your first post:
local function usernameLength( event ) if username.text \> 10 then local alert = native.showAlert( "Username is to long ", "Your username cannot be longer than 130 characters",{ "Try again" } ) return true end end
I can see immediately that there are a couple things wrong here:
-
usernameLength(event) - This looks like it is a listener and not a function that is called by you directly.
- username.text - username is clearly a global which indicates you’re having trouble setting up safe and proper scope.
These observations tell me you are trying to write a text input listener, and you want it to stop the user from entering too many characters for the field.
To do so, I would do this :
local defaultField local maxLen = 10 local function textListener( self, event ) -- Cap length at maxLen characters. If user types maxLen+1 characters, the end char -- will automatically be removed local text = event.text if( string.len(text) \> maxLen ) then -- Is the string longer than maxLen? If so... self.text = string.sub( text, 1, maxLen ) end end -- Create text field defaultField = native.newTextField( 150, 150, 180, 30 ) defaultField.userInput = textListener defaultField:addEventListener( "userInput" )
While it may seem like a good idea to pop-up a message to warn the user, it may not be a great idea because I’m not 100% sure how that will affect the text field (also a native object) interaction. I think simply removing extra characters will be good enough. The user will see this happening.
Advice
- Since you are a new programmer and Lua is new to you - AND - because Lua is the foundation language for Corona users, you need to learn it before using Corona or writing games. That would have helped a lot in this case and others.
I suggest you take a short break from your game and go read the PIL: https://www.lua.org/pil/contents.html
Specifically, I suggest you read and type in the example code for these chapters and sub-chapters:
- 1 … 5 - Fundamentals
- 11.1 - Arrays
- 18 - math lib
- 19 - table lib
- 20 - string. library (would have helped with this question)
- 21 - io.* library
-
Grab a book and follow along. I strongly suggest you read through at least one Corona Dev book (Try Dr. Burton’s site or Amazon)
-
Take a programming class (any language). If you get the opportunity to take a programming class at school, go for it. This will help a lot. I promise. It is incredibly hard to write games and program starting from zero or nearly zero. I strongly believe that all programmers should start with a formal education and then branch out. Otherwise you risk having gaps in your knowledge that will hobble you for life.
4. Spend more effort on your responses and posts.
You may have noticed that some of us (me included) get exasperated with you. For me, this is because I spend real time and effort answering you. I typically:
- Type long clear answers
- Use proper spelling, punctuation, sentence structure, paragraphs, …
- I provide clear and well formatted code in code blocks.
- Go back and re-edit me post if there is something wrong or poorly formatted
- even make videos sometimes.
Then, you respond with a one-liner like, “It didn’t work.”
This makes me want to pull out my hair. It also makes me NOT want to help you. So, as my final word of advice. Please realize that short answers like that are seen as disrespectful and entitled behavior and do not encourage folks to help.
Again, I want you and other Corona developers to be successful and I really want to help when I can, so as I’ve said in other places to other folks, “Help me to help you.”
Cheers,
Ed