1

So, I have a very specific problem. I have a random word which I have already generated. I also have a separate string that uses the length of that character, 'n' to repeat "_ " n times to field wordDisplay. These elements of the program work. So, if the random word was "meme", the wordDisplay, on openCard would show "_ _ _ _ ".

Now, I have an input button "a". If this button is clicked, it will check for "a" in the randomword variable and if it is found, it will replace the "_ " in the wordDisplay with "a". So, if the randomword was "name", then before you click "a", the wordDisplay shows "_ _ _ _ ". After you click "a", wordDisplay will show "_ a _ _ ". However, if there is more than one "a", it will reveal all of them.

It is also worth knowing that I plan to have the program do something if there is no letter found.

Being a beginner to LiveCode, this all seems very confusing to me and I would really appreciate someone to share their knowledge on how I would do something like this. Thanks!

notHalfBad
  • 213
  • 2
  • 9

1 Answers1

1

There are several ways to do this, but I wouldn't try to do everything in a single field. Unless you use a monospaced font, the text's width will shift each time you swap characters and underscores.

I would suggest using a separate field (or button or graphic) to display each character of the word (named something like letter1, letter2, letter3...), and place a line graphic under each field (named line1, line2, line3...). Create a number of controls that matches the length of your worst-case word, and hide the unneeded controls each time a new word is selected.

By laying the controls out this way, you're guaranteed a relatively consistent alignment regardless of what font is used, or how the card is scaled.

Now you can simply toggle the visibility of each "letter" field and the visibility of each "underscore" line as needed, by looping through the set of controls. Something sort of like:

[ the secret word is 'aardvark' ]

command checkForCharacterMatch pUserChar
   local noMatch = true
   repeat with N = 1 to length(theSecretWord)
      put long id of field ("letter" & N) into theLetter
      put long id of graphic ("line" & N) into theLine
      if (the text of theLetter = pUserChar) then
         show theLetter
         hide theLine
         put false into noMatch
      end if
   end repeat
   if noMatch then
      -- DO PENALTY STUFF HERE
   end if
end checkForCharacterMatch

In reality, you don't even need to hide the lines, but this follows your original approach.

Scott Rossi
  • 885
  • 5
  • 6
  • Where would I put this code? I can't seem to be able to google the 'command' command and find anything (I've never used it before). Would I put this in the openCard script and call it using something like `command checkForCharacterMatch 'a'`? Thanks for your help. – notHalfBad May 19 '16 at 23:04
  • "Command" is just a more descriptive way of writing "on" (like "on mouseUp") but for custom handlers. And yes, the card script is a good location for the script. And whether you're using a set of letter buttons on the card or an on-screen keyboard, you simply pass the letter entered to the handler: checkForCharacterMatch "b". FYI, the above isn't tested, just a suggested starting point. – Scott Rossi May 20 '16 at 08:01