9 Replies Latest reply on Jul 28, 2008 1:59 PM by Martin Schaefer

    Text Input - Entering and Checking

    andyulrika_is_back Level 1
      Hello

      Could someone please tell me how to implement the following.

      I want to have a text input box for a user to enter text or a number. I then want them to click on a check answer box which compares what they have written to an answer and then display a correct or wrong graphic.

      Any help much appreciated.

      Andy
        • 1. Re: Text Input - Entering and Checking
          Level 7
          You can get the contents of a #text member like this:

          put member("yourTextMemberName").text

          You can compare that text to a stored value like this:

          if member("yourTextMemberName").text="Correct Answer" then
          --do something if correct
          else
          --do something if incorrect
          end if

          It is important to note that the .text of a member will always be a
          STRING. Therefore if the user had entered the number 4, it will return
          the value as "4".
          • 2. Re: Text Input - Entering and Checking
            andyulrika_is_back Level 1
            Hi, thanks for the input.

            I tried this out but couldn't get the code to work...

            Here is what I have so far -

            I have a text input button on the stage called TextAnswer
            I have a text cast member called TextAnswer1 with the number 5 inside.
            On the stage is a button with the following code attached to run the script.

            on mouseUp me

            on mouseUp me

            if member("TextAnswer").text= member("TextAnswer1").text then
            sound(3).play(member("wrongchoice"))
            else
            sound(3).play(member("welldone"))
            end if

            However, it appears as if there is no communication between the cast members.

            Any help appreciated.

            Regards

            Andy
            • 3. Re: Text Input - Entering and Checking
              Level 7
              So what exactly is happening? Are you getting error? Does it always play
              "wrongchoice" no matter what you type in? Or does it always play "welldone"?

              Why do you have

              on mouseUp me

              on mouseUp me


              Is it really that way in your script, or is it a typo.
              • 4. Re: Text Input - Entering and Checking
                Level 7
                Aren't there only 2 sound channels? Try

                sound(1).play(member("wrongchoice"))
                • 5. Re: Text Input - Entering and Checking
                  Level 7
                  I thought you might be interested in learning a more efficient method of
                  doing this. What you are doing will work but it has some disadvantages.
                  It appears you are storing the correct answer in a cast member. This
                  means you will need a unique cast member for each answer. Also your
                  script is using hard coded member names, which means you will need a
                  unique script for each and every question. What you will find is that
                  each script for the questions is almost identical, except for the member
                  names.

                  With Lingo you have the ability to define your own custom "properties"
                  that allow you to reuse the same behavior on every question. It won't
                  matter if you have 1 question or 100, you really only need one behavior.

                  The trick is to use a function called "getPropertyDescriptionList". This
                  allows you to define any number of properties that you need for a
                  sprite. When you apply the behavior to a sprite, Director will prompt
                  you with a dialog box that gives you the opportunity to assign the
                  unique values to the properties, such as the correct answer for this
                  particular question. You can change the value of any parameter later
                  through the property inspectors behavior tab if you need to. It also
                  allows you to define default values for your properties. So for example
                  your behavior can play a sound called "wellDone" for 99 questions by
                  default, but could play a sound called "greatJob" for one particular
                  question.

                  Trust me, once you understand how to do this you will wonder how you
                  ever got along without it. It is not difficult at all either. So here a
                  example, implementing the functionality you are trying to create. Using
                  just the two behaviors below, one "check answer" button cast member and
                  one text member for user input, you can create a quiz with any number of
                  questions.

                  So here is the behavior you attach to your check answer button. When
                  Director prompts you, set the properties or accept the defaults.
                  Warning, long lines in this code will get word wrapped in your news
                  reader or browser. When you paste it into Director you will have to
                  figure out where these line breals got inserted and remove them.

                  ---
                  property correctInput, correctSoundMember, incorrectSoundMember,
                  inputTextMember, soundChannelNum

                  on mouseUp me
                  if member(inputTextMember).text = correctInput then
                  sound(soundChannelNum).play(member(correctSoundMember))
                  else
                  sound(soundChannelNum).play(member(incorrectSoundMember))
                  end if
                  end

                  on getPropertyDescriptionList me
                  description = [:]
                  addProp description,#correctInput, [#default:"default",
                  #format:#string, #comment:"Expected user input"]
                  addProp description,#correctSoundMember, [#default:"wellDone",
                  #format:#string, #comment:"Sound member name to play if correct"]
                  addProp description,#incorrectSoundMember, [#default:"wrongChoice",
                  #format:#string, #comment:"Sound member name to play if correct"]
                  addProp description,#inputTextMember, [#default:"inputTextMember",
                  #format:#string, #comment:"Name of the input text member"]
                  addProp description,#soundChannelNum, [#default:1, #format:#integer,
                  #comment:"The sound channel to play"]
                  return description
                  end
                  ---

                  You can reuse the the same text input member for each question, but will
                  need a simple behavior to clear it out between questions.

                  ---
                  property spriteNum

                  on beginSprite me
                  sprite(spriteNum).member.text = ""
                  end

                  on endSprite me
                  sprite(spriteNum).member.text = ""
                  end
                  ---

                  If you prefer I can email you a simple director movie demonstrating
                  this. I guarantee you that doing it this way will save you a lot of time
                  and effort.

                  • 6. Re: Text Input - Entering and Checking
                    andyulrika_is_back Level 1
                    Hi Dave

                    Thanks very much for your help. I've been trying out your script but I when I enter 5 as my expected answer, attach the script to my 'check' button and enter 5 in the TextInput button it only plays the 'wrongchoice' audio file instead of the 'welldone' audio file. Which makes me think there is something I'm doing wrong.

                    By the way, the 'on mouseup me' duplication in previous post was a typo!

                    Is there any chance you could forward your demo movie for me to compare against mine.

                    Thanks in advance.

                    Andy

                    • 7. Re: Text Input - Entering and Checking
                      Level 7
                      Here is a dir file which uses the behavior with just one question where
                      the answer is 5. It works for me with no problem. Let me know if it
                      works for you.

                      I have uploaded it to mediafire which provides free (ad sponsored) file
                      hosting.

                      http://www.mediafire.com/?xjd5thks2mx



                      andyulrika_is_back wrote:
                      > Hi Dave
                      >
                      > Thanks very much for your help. I've been trying out your script but I when I
                      > enter 5 as my expected answer, attach the script to my 'check' button and enter
                      > 5 in the TextInput button it only plays the 'wrongchoice' audio file instead of
                      > the 'welldone' audio file. Which makes me think there is something I'm doing
                      > wrong.
                      >
                      > By the way, the 'on mouseup me' duplication in previous post was a typo!
                      >
                      > Is there any chance you could forward your demo movie for me to compare
                      > against mine.
                      >
                      > Thanks in advance.
                      >
                      > Andy
                      >
                      >
                      >
                      • 8. Re: Text Input - Entering and Checking
                        andyulrika_is_back Level 1
                        Hi Dave

                        Thanks for helping me out. Works a treat. I exchanged the TextInput Flash component for a regular editable text member and this seems to have fixed the problem.

                        All the best.

                        Andy
                        • 9. Re: Text Input - Entering and Checking
                          Martin Schaefer Level 1
                          Hi Andy,

                          if you use Flash TextInput Component, you want to check sprite(x).text and not member(x).text.

                          Cheers,
                          Martin