11 Replies Latest reply on Jul 28, 2009 10:39 AM by Grover1

    Simple if / then statement

    Grover1 Level 1

      I have a variable “textInput” that contains a string of letters derived from a user's text input.  I would like to set up an if/then statement or case statement that sends the user to a “congratulations” marker if their input was the lower-case letter "a" and send them to a “try again” marker if their input was otherwise.  Thanks for helping with an issue this basic,

      David

        • 1. Re: Simple if / then statement
          Chunick Level 3

          What do you have for code so far?... where have you gotten stuck?... maybe your code that you've written so far is almost there and just need a few changes or tweaks. Could you please post it. Thanks.

          • 2. Re: Simple if / then statement
            Grover1 Level 1

            Chunick,  This is what I've got so far.  I can't figure out how to format the "if textInput = "a" part.  I'm also unsure if I should initialize textInput as a string or not.  The code, if you could call it that, is at the end of that which gets the user's input and places it in the variable, "textInput."

             

            if

             

             

             

             

             

             

             

            textInput = "a" then

              go

             

             

             

             

             

             

             

            to "correct" else

              go to "incorrect"

             

             

             

             

             

             

             

             

            end if

             

             

            Pardon if the spacing and line placement of the code are screwy.  I have tried to edit it several times, but it jumps around the page.

            thanks,

            -David

             

             

             

             

             

             

             

            • 3. Re: Simple if / then statement
              Chunick Level 3

              textinput does not need to be initialized as a string... based off of your previous post titled, "User input to variable", and this post I'm guessing that your code should look something like this:

               

              property spriteNum
              property my
              property myMember

              on beginSprite me
                my = sprite(spriteNum)
                myMember = my.member
                myMember.editable = 1
              end

              on keyDown me
                kc = _key.keyCode
               
                case kc of
                  36: -- ENTER
                    -- you don't need a global, in this case     
                    _global.textInput = myMember.text
                    textInput = myMember.text
                    -- you don't have to format the if..then part
                    -- or maybe I'm not understanding what you want
                    if textInput = "a" then
                        go to "correct"
                    else
                        go to "incorrect"
                    end if
                    stopEvent
                  otherwise:
                    pass
                end case
              end
              • 4. Re: Simple if / then statement
                Grover1 Level 1

                Chunick,

                 

                That worked like a charm.  Very grateful.

                 

                -David

                • 5. Re: Simple if / then statement
                  Sean_Wilson Adobe Community Professional
                  That worked like a charm.

                  Except that it would also work if you entered uppercase "A" when you specifically asked about lowercase "a". Try the following amendment to Josh's amendment of my original :-)

                  property spriteNum
                  property my
                  property myMember
                  
                  on beginSprite me
                    my = sprite(spriteNum)
                    myMember = my.member
                    myMember.editable = 1
                  end
                  
                  on keyDown me
                    kc = _key.keyCode
                    
                    case kc of
                      36: -- ENTER
                        textInput = myMember.text
                        -- string comparison is case-insensitive, but
                        -- using a list makes the comparison operator obey case
                        if [textInput] = ["a"] then
                          go to "correct"
                        else
                          go to "incorrect"
                        end if
                        
                      otherwise:
                        pass
                        
                    end case
                  end
                  
                  1 person found this helpful
                  • 6. Re: Simple if / then statement
                    Grover1 Level 1

                    Sean, much thanks.  I was trying to figure out how to do that when I

                    received your message. A room full of chimps would have accidently typed the

                    complete works of Shakespeare before I could have figured all of this out.

                    -David

                    • 7. Re: Simple if / then statement
                      Grover1 Level 1

                      PS: Any idea how to clear the user's input before they exit the text input

                      frame?

                      -David

                      • 8. Re: Simple if / then statement
                        Chunick Level 3

                        Grover1 wrote:

                         

                        PS: Any idea how to clear the user's input before they exit the text input

                        frame?

                        -David

                         

                        myMember.text = ""

                         

                        ... but to make it more challenging, I'm going to let you figure out where it goes in the code that's previously been posted.

                         


                        P.S. There are some great books on Director and Lingo still floating around... you can probably start by searching Amazon.com

                        1 person found this helpful
                        • 9. Re: Simple if / then statement
                          Grover1 Level 1

                          Chunick,  Thanks for framing the problem within my skill set.  Got it on the first try.

                           

                          Actually, I've felt guilty having you guys do all the work.  The most I'd hoped for was a point in the right direction. I've already watched and took notes on 8+ hours of James Gonzales' VTC video, but much of the stuff that I need to do was not covered.  I know that answering my every question is too much to ask from any given source; I'll hit Amazon and order everything in sight.

                           

                          Best regards, David

                          • 10. Re: Simple if / then statement
                            Chunick Level 3

                            Grover1 wrote:

                             

                            Chunick,  Thanks for framing the problem within my skill set.  Got it on the first try.

                             

                            Actually, I've felt guilty having you guys do all the work.  The most I'd hoped for was a point in the right direction. I've already watched and took notes on 8+ hours of James Gonzales' VTC video, but much of the stuff that I need to do was not covered.  I know that answering my every question is too much to ask from any given source; I'll hit Amazon and order everything in sight.

                             

                            Best regards, David

                            No problems. Don't feel guilty, just keep on learning! It's the people that really ARE looking for a handout that should feel guilty... if you're honestly struggling but are giving it an honest shot then a book will definitely help. Use all the resources you can, including here.

                            • 11. Re: Simple if / then statement
                              Grover1 Level 1

                              Chunick, thanks again for your help. I went to Amazon and ordered everything

                              that seemed Lingo oriented.  I'll make sure to exhaust every resource first.

                              Best, David