...replace blanks in a string with typed characters?

0 favourites
  • 3 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I have a text object (txt_fact) displaying a sentence that includes one word replaced with a line of blanks. I want the user to be able to replace the blanks with the missing word, or at least write on top of the blanks.

    Currently I use the following method:

    1. Add a second text object (txt_answer) that sits over the blanks in txt_fact.

    2. Add a textbox object with the CSS set to render it invisible.

    3. Focus the textbox and set txt_answer to display whatever text is typed into the textbox.

    This would work fine, except fonts in a text object display differently depending upon the browser/device, so I can't guarantee that the position of txt_answer will always lie precisely over the blanks.

    Is it possible to derive the coordinates of characters in a string? e.g. find x,y for the 3rd "_" in "I laughed at the _______ dog."

    Failing that, is this something I can handle with regex? I figured I might be able to use a replace expression, but I'm unsure how to handle the user deleting characters that have been added once the blanks have been replaced.

    Or maybe there is another way?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Finding screen coordinates for a character in Text object is almost impossible, because browsers render text differently. You can do it with SpriteFont, but this will still be a difficult task.

    I suggest using find() expression to search for the first "_" character. If player types the correct letter, replace this "_" with the letter:

    Set n to find(MyText, "_")
    Set MyText to left(MyText, n) & TypedLetter & right(MyText, len(MyText)-n-1)
    

    Then repeat for the next letter and so on. Demo:

    dropbox.com/s/p4gydwzsx9jm2j4/FillTheBlankWord.capx

  • Big thanks dop2000,

    Your demo pointed me in the right direction - I ran with it and came up with this:

    dropbox.com/s/jhxbs17qufnjdh5/FillTheBlankWord2.capx

    This version also allows you to delete letters and then test the word by pressing enter.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)