Function to Replace Letters

I need some help with the following:

I will have a word displayed on the screen. Let’s say the word is: TREE

I would like some way of splitting the letters up programatically so that each letter (T R E E) of the word will be touchable separately and that I’ll know which one was touched.

Does anyone know of a way to split up a word like this and make each letter touchable, but also keep it so I can match the word to a dictionary (so don’t destroy the word, so I can still read it)?

Any help, direction, or the smallest piece of info for me to get started would be greatly appreciated.

Thanks,

–Matt [import]uid: 14084 topic_id: 6560 reply_id: 306560[/import]

You’re mixing up the display objects with the data that you match to the dictionary. The images being displayed on the screen can be a series of text objects or a bunch of image objects or really whatever you want, and then simply store in a variable the string “tree” so the computer knows what word is currently being displayed.

The real trick is displaying the series of characters based on the string, not storing the string. You basically want a function that iterates over each character in the string and creates a new image each time.

psuedocode

[code]
local function displayLetters(text)
for each char in text
newText(char)
end
end

local screenText = “tree”
displayLetters(screenText)
[/code] [import]uid: 12108 topic_id: 6560 reply_id: 22756[/import]

Okay, that’s making more sense. Thanks for giving me a direction to go in! I’ll work on my code and post with any issues.

Thanks again!

–Matt [import]uid: 14084 topic_id: 6560 reply_id: 22770[/import]

Okay. I’ve tried a few different things now to split a string. All I need is the individual characters out of it. I even tried a split function from the lua.org site. It kept giving me an error about expecting a Proxy and receiving nil. Don’t understand what that means.

Thank you for the code jhocking… I didn’t understand it like I thought I did I guess. I’m still VERY new at Lua and Corona. Only thing I’ve done even a little bit with is some Ruby. And it’s not too much that I’ve done.

So I wasn’t able to split out the individual letters in that example string TREE. Sorry, I’ve been researching it so I don’t have to bother people on this site, but haven’t come up with any solutions. If anyone has any solutions, please let me know.

–Matt [import]uid: 14084 topic_id: 6560 reply_id: 23035[/import]

Use the substring function to get individual letters from the string:
http://lua-users.org/wiki/StringLibraryTutorial [import]uid: 12108 topic_id: 6560 reply_id: 23069[/import]

Thanks jhocking!

Okay, so I have this code:

[lua]local word = “tree”

letter = string.sub(word, 1, 1)

local letters = display.newText(letter, 25,25,nil,25)[/lua]

And that works out great. I get a “t” back like I should.

I want to have that word variable to be filled by a dictionary. It’s only “tree” for these examples. So the word length will change.

I want to put the letters in a table. That way I can call them with letter[1], letter[2], etc?

I have tried this, and it doesn’t work, but I want to know if I’m even close to being on the right track:

[lua]local word = “tree”

wordLength = string.len(word)

letter = {}

for c = 1, wordLength do
letter = string.sub(word, wordLength, wordLength)
end

local letters = display.newText(c, 25,25,nil,25)[/lua]

I know I’m messing up in a few places. Just wanted to see if a table is possible so I can call them how I want.

Thank you so much for your help on this. [import]uid: 14084 topic_id: 6560 reply_id: 23091[/import]

That’s the right idea, yeah. But you have a couple mistakes in the code, including:

  1. You put the newText() line outside the “for” loop. That should be in the loop.

  2. You stick the letter into the variable “letter” but then never use that variable. You want display.newText(letter), not display.newText© [import]uid: 12108 topic_id: 6560 reply_id: 23285[/import]

Awesome! You’re the man.

So now I have this:

[lua]local word = “tree”

wordLength = string.len(word)

letter = {}

for c = 1, wordLength do
letter = string.sub(word, wordLength, wordLength)
local letters = display.newText(letter, 25,25,nil,25)
end[/lua]

What I get back from it is the letter ‘e’. I’m not sure if there is a way to show the whole word?

Thanks again for all your help. I know I’m a pain. I’m trying to mess with the code alone and figure this out by myself. But I keep getting errors when I change things. :slight_smile: [import]uid: 14084 topic_id: 6560 reply_id: 23296[/import]

Woops! Looks like I needed to do:

[lua] letter = string.sub(word, c, c)[/lua]

Instead of:

[lua] letter = string.sub(word, wordLength, wordLength)[/lua]

This way it moves through each letter and not only through the last one… Okay, so now that I have that fixed, I have the word ‘tree’ but each letter overlaps the letter previous.

I know this is due to the line:

[lua] local letters = display.newText(letter, 25,25,nil,25)[/lua]

But don’t know how to change those positions each time so the word is spaced out. I hope I’m making sense. Any help will be appreciated. [import]uid: 14084 topic_id: 6560 reply_id: 23304[/import]

Instead of using the number 25 every time, you want the position to be dependent on “c” (since “c” is the value that changes.) So like display.newText(letter, 25 + c * 25, 25, nil, 25) [import]uid: 12108 topic_id: 6560 reply_id: 23308[/import]

PERFECT! You’re awesome. Thanks for all of your help. Hopefully I won’t be bugging you in the future. This was the part I was having the most difficult time with. Now I can move on.

Thanks again,

–Matt Royer [import]uid: 14084 topic_id: 6560 reply_id: 23312[/import]