14 Replies Latest reply on Jun 17, 2008 8:39 AM by Mr.Wallace

    IF and ELSE Statements

    Mr.Wallace
      Man...I'm so close to figuring this out and I didn't want to have to come back to forum for this because I've had a couple topics on here already when I was starting this project and half way through it and now towards the end of it, LOL! I hope y'all don't mind!?

      Anyways...

      Now, I'm trying to get users to fill in 4 input boxes with the correct info, then hit the ENTER key to proceed to the next frame. Everything is working perfectly with that part of my code, however, it's the part about if they enter in the wrong info that I'm getting stumped on.

      I've tried ELSE statements and various forms of listeners and IF statements but I just can't seem to get it to work.

      When they enter in the wrong info, in any of the input boxes, I need to to be taken to a different frame. I had a script in there that kind of worked but instead of it waiting until they entered in all the info, it would just go automatically to the "wrong" frame because technically they were only getting a chance to enter in the first letter of the first word.

      Anyways, the code I'm using now is attached. It works fine except the part of the wrong info being entered. If a user just comes to this frame (where this code is located) and hits ENTER, thn they are taken to the "Incorrect" frame, which is fine because technically they didn't enter anything into the 4 boxes. But. if they did start typing into a box and then hit ENTER, they should be taken to the same "Incorrect" frame because they didn't enter everything, but it doesn't do that. Even if they enter info into every box and one letter is missing for example, that's wrong and should go to that "Incorrect" page, but doesn't.

      Any ideas?
        • 1. IF and ELSE Statements
          Rothrock Level 5
          Well I think you are going to have some problems because you are using two listeners and they are overwriting each other. But I think you need to take a different approach in any event. Since you aren't trying to validate as you go, the only thing you really need to listen for is the enter key, when that is pressed you can check to see if things are correct and respond accordingly. So make your listener:

          var listener:Object=new Object();
          listener.onKeyDown=function(){
          if (Key.isDown(Key.ENTER)){
          evaluate();
          }
          }

          So only when the enter key is pressed will we call the evaluate function. So now you coudl use something like this:

          function evaluate(){
          if(box1.text==""info1" && box2.text=="info2" && so on){
          trace ("correct");
          } else {
          trace("incorrect");
          }
          }

          Notice I don't need to check for the incorrectness. If it isn't correct then it is incorrect. Also the code you gave above for your incorrect conditional is not right. You are using ands which mean that box1 is not info1 and box2 is not info2 and so on. So in effect your second conditional is only true if all of them are wrong. I think you would want || (which means OR). That way if any of them are wrong the statement is true. But as I said you don't need to check that in this case.

          But here is another one for you. What if suddenly you have to add two new text boxes to this? The conditional get hairier and hairier. So a different approach could be something like this. Change the names of your text boxes to start counting with zero and make an array of answers:

          var correctInputs:Array=new Array("info0","info1","info2",...);

          Then in your evaluate function you can do this:

          var entryIsCorrect:Boolean=true;
          for(var i=0;i<correctInputs.length;i++){
          if(this["box"+i].text != correctInputs[ i ]){
          trace("box"+i+" doesn't equal "+ correctInputs[ i ]);
          entryIsCorrect=false;
          }
          }
          if(entryIsCorrect){
          trace("Correct entries all around")
          } else {
          trace("There was at least one error");
          }

          That way all you need to do if the number of boxes changes is to add a box to the stage and add it's correct entry to your array. Snazzy, huh?
          • 2. Re: IF and ELSE Statements
            Rothrock Level 5
            PS: I forgot to mention that when you are done listening for the enter key you need to removeListener so that your evaluate function isn't called again if they press the enter key.
            • 3. IF and ELSE Statements
              Mr.Wallace Level 1
              WOW! Maybe a little too snazzy for me, LOL!

              So, is this the way I should have the code laid out?

              I don't think I need the TRACE in there do I? I want users to be taken to either the frame that is labelled CORRECT if they are right and if they are wrong, they get taken to the frame labelled INCORRECT.

              • 4. IF and ELSE Statements
                Gaz.T.
                You don't need the Trace commands, (that just prints 'correct' or 'incorrect' to your output box, useful for debugging).

                You can replace them with gotoAndStop("CORRECT"); and gotoAndStop("INCORRECT"); If you just want to jump to those frame labels and stop.

                I think, like rothrock says, you'll also need to add

                Key.removeListener(listener);

                to switch the listener off when after you've done your evaluation.


                • 5. Re: IF and ELSE Statements
                  Mr.Wallace Level 1
                  Man...this is killing me that I can't figure it out...it seems to be an easy answer.

                  This code still isn't working for me for some reason. Tried a bunch of different ways but still nothing. When I run the preview projector and fill in the 4 boxes with either the right or wrong answers, nothing happens.

                  Here is the script I used:

                  • 6. Re: IF and ELSE Statements
                    Mr.Wallace Level 1
                    Geez, I'm starting to think that I'm not going to be able to do this...
                    • 7. Re: IF and ELSE Statements
                      Rothrock Level 5
                      Well add some traces to help you see the progress. (They won't work in the projector, but should in the testing environment.)

                      And that "so on" part is to actually be filled out by you with the rest of the conditionals. So if you haven't done that then do so.

                      So add those traces. Run it again and see if you can figure out what is happening. If not, cut and paste the traces here.
                      • 8. Re: IF and ELSE Statements
                        Mr.Wallace Level 1
                        ...damn...still nothing man. I didn't even get the Output Panel to pop up, didn't read the traces at all. I created a new movie just to test the script out and it doesn't work for me. I named the boxes accordingly in the 'Instance Name' boxes; also tried to name them in the 'Variable' box; even tried putting labels in both "Instance' and 'Variable' boxes, nothing worked. This is exactly the script I used...almost exactly as you showed me Rock:



                        • 9. Re: IF and ELSE Statements
                          Rothrock Level 5
                          Did you add the listener?

                          Key.addListener(listener)

                          You won't be listening for any keys if you don't do that!

                          And don't use the variable box. That just messes everything up.
                          • 10. Re: IF and ELSE Statements
                            Mr.Wallace Level 1
                            Ok, first I'm sorry that I'm not grasping this stuff too quickly, my apologies. Second, I'm still not getting the flow I'm looking for.

                            However, the script does seem to work. If the wrong info is now entered into the boxes, it takes the user to the "Incorrect" frame, and when the right info is entered in, it'll take to the "Correct" frame. PERFECT! After adding the KeyListener was added (thanks Rock), the traces work and everything is good. I removed the first "trace" just because it was stopping me from entering any other key presses after the first one, and it was working fine. So things do work but...with two snags though.

                            Once on the screen where you need to enter info into the 4 boxes, if the user just hits ENTER before entering anything into the boxes it will still accept that as correct and go to the "Correct" frame.

                            The other thing is, once we get to either the "Incorrect" or "Correct" frame(s), the listener is still active. I tried to add a removeListener to the end of the script on the enter page as well as on the "Correct" or "Incorrect" pages. It still reads the listener.

                            Sorry people but, again I have to ask, any ideas? Here is my code:

                            • 11. Re: IF and ELSE Statements
                              Rothrock Level 5
                              About the pressing enter issue. When that happens, do you get the correct trace? If you don't then it isn't because it thinks it is correct. If you are in the testing environment be sure you've gone up to the Control menu and select Disable Keyboard shortcuts or something like that. In the testing environment, pressing enter tells the player to play so that could be the no entry and enter equals correct problem.

                              As for removing the listener, it depends upon when you want to stop listening. I would think maybe inside the conditional or your onKeyDown handler. Right before the evaluate call:

                              Key.removeListener(Listener);


                              • 12. IF and ELSE Statements
                                Mr.Wallace Level 1
                                HA! You were right Rock, I tested the projector and the ENTER function works perfectly!

                                Well...almost, LOL!

                                If you fill in the wrong info, it takes you to "Incorrect", beautiful. Fill in the right info and it takes you to "Correct", sweet! However, the frame labelled "Incorrect" comes after the frame called "Correct" in my movie. Now what's happening is that when the entered info is correct and it goes to "Correct", the removeListener still isn't stopping the user from hitting ENTER again and going to the next frame, "Incorrect". I put it in the KeyDown function, I put it at the end of the script, I put it at the beginning of the next frame...can't seem to stop it. I even made another Scene and put it on there, nothing.

                                But other than that everything is working GREAT!
                                • 13. Re: IF and ELSE Statements
                                  Rothrock Level 5
                                  That is strange.so your code looks like this (plus all the other stuff that was already there?:

                                  if (Key.isDown(Key.ENTER)) {
                                  trace("Talk to the hand, because I'm not listening anymore.");
                                  Key.removeListener(listener);
                                  trace("That time it was the enter key, so I should evaluate.");
                                  evaluate();
                                  }

                                  That should work. I just tested it and it works for me. Well, it worked on the second try, the first time I misspelled "listner," so be sure that you have everything spelled correctly!

                                  Other than that are you sure it is the evalute function that is getting called? How do you know? Is there any other code that could be causing the playhead to move?

                                  BTW, if you are using Actionscript then you shouldn't use Scenes. There are some strange things that happen with that and it just isn't worth it!
                                  • 14. Re: IF and ELSE Statements
                                    Mr.Wallace Level 1
                                    Rock, you are AMAZING!

                                    I did have a spelling error as well and I put the listener where you put it and it looks like everything is working smoothly and beautifully!

                                    I can't thank you enough for your patience and efforts...thank you so, so, SO much! Your help is greatly appreciated!!!

                                    Mr. Wallace