10 Replies Latest reply on Sep 29, 2006 7:32 AM by kglad

    "if", "else" and "if else" statements

    twistedpancreas Level 1
      Ok I'm not sure if i've stuffed this up but this is my 1st go at trying to utilise the "if", "else" and "if else" statements, after going through it in the "Actionscript for Flash 8" book.

      What I have is a science student character standing outside a lab door. Hanging on a wall beside him is a coat and some goggles. Now the user can click on the coat, goggles or door. Depending on what people click on will send them off to different animations.

      And so here is what AS I have:

      stop();

      coatHangerBtn._visible = 0;
      charGogglesBtn._visible = 0;
      charCoatBtn._visible = 0;

      charGogglesHangBtn.onRelease = function() {
      charGogglesHangBtn._visible = 0;
      charGogglesBtn._visible = 1;
      var charGoggles:Boolean = true;
      trace(charGoggles);
      };
      charGogglesBtn.onRelease = function() {
      charGogglesHangBtn._visible = 1;
      charGogglesBtn._visible = 0;
      var charGoggles:Boolean = false;
      };
      charCoatHangBtn.onRelease = function() {
      charCoatHangBtn._visible = 0;
      charCoatBtn._visible = 1;
      charClothesBtn._visible = 0;
      var charCoat:Boolean = true;
      };
      charCoatBtn.onRelease = function() {
      charCoatHangBtn._visible = 1;
      charCoatBtn._visible = 0;
      charClothesBtn._visible = 1;
      var charCoat:Boolean = false;
      };

      var charGoggles:Boolean = false;
      var charCoat:Boolean = false;

      labDoorBtn.onRelease = function() {
      if (charGoggles && charCoat) {
      gotoAndPlay("correct");
      } else if (charGoggles) {
      gotoAndPlay("goggles");
      } else if (charCoat) {
      gotoAndPlay("coat");
      } else {
      gotoAndPlay("nothing");
      }
      labDoorBtn.gotoAndPlay(2);
      };
        • 1. Re: "if", "else" and "if else" statements
          Level 7

          "twistedpancreas" <webforumsuser@macromedia.com> wrote in message
          news:effm1i$68e$1@forums.macromedia.com...
          > Ok I'm not sure if i've stuffed this up but this is my 1st go at trying to
          > utilise the "if", "else" and "if else" statements, after going through it
          > in
          > the "Actionscript for Flash 8" book.
          >
          > What I have is a science student character standing outside a lab door.
          > Hanging on a wall beside him is a coat and some goggles. Now the user can
          > click
          > on the coat, goggles or door. Depending on what people click on will send
          > them
          > off to different animations.
          >


          Without going through all of the code.. this did jump out at me.

          > labDoorBtn.onRelease = function() {

          Here you say if charGoggles evaluates to true... goto and play from frame
          label correct.
          the you say, else if charGoggles evaluates to true.... goto and play
          goggles.

          That is like saying:
          if (2 + 2 == 4) {
          do something;
          } else if (2 + 2 == 4) {
          do something else;
          }

          the 'do something else' will never get evaluated.

          > if (charGoggles) {
          > gotoAndPlay("correct");
          > } else if (charGoggles) {
          > gotoAndPlay("goggles");
          > } else if (charCoat) {
          > gotoAndPlay("coat");
          > } else {
          > gotoAndPlay("nothing");
          > }
          > labDoorBtn.gotoAndPlay(2);
          > };
          >

          Also, further up in the onRelease handlers you are setting up a local
          variable charGoggles when you use the line
          var charGoggles:Boolean = whatever;

          instead, defined charGoggles up where you define
          charCoatBtn._visible = 0; // like
          var charGoggles:Boolean = false;

          Then, in the onRelease handlers you can use charGoggles without the var and
          dataType.



          • 2. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
            kglad Adobe Community Professional & MVP
            no, the logic's ok. the first conditional is testing if both booleans are true. that will resolve to false and one of the "else if" statements will resolve to true if one condition is true and one is false.

            addendum: i see twisted edited his original message and corrected the problem smb identified.

            the problem is making those booleans local to the buttons so they are undefined (or false) when the if-statements execute.

            to remedy, remove the prefix "var" that precedes your booleans.
            • 3. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
              Level 7
              The logic is not ok, SMB is correct. The code will never get to frame
              "goggles" because if charGoggles is true, you will always, and only, get to
              frame "correct". Not sure what the OP is trying to do, as there was no
              actual question, but it will need to be done a bit different.

              --
              Dave -
              Adobe Community Expert
              www.blurredistinction.com
              http://www.adobe.com/communities/experts/


              • 4. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
                Level 7

                "kglad" <webforumsuser@macromedia.com> wrote in message
                news:effti4$e20$1@forums.macromedia.com...
                > no, the logic's ok. the first conditional is testing if both booleans are
                > true. that will resolve to false and one of the "else if" statements will
                > resolve to true if one condition is true and one is false.
                >
                > the problem is making those booleans local to the buttons so they are
                > undefined (or false) when the if-statements execute.
                >
                > to remedy, remove the prefix "var" that precedes your booleans.
                >kglad,

                Forgive my ignorance here... but regarding the onRelease handler below:

                labDoorBtn.onRelease = function() {
                if (charGoggles) {
                gotoAndPlay("correct");
                } else if (charGoggles) {
                gotoAndPlay("goggles");
                } else if (charCoat) {
                gotoAndPlay("coat");
                } else {
                gotoAndPlay("nothing");
                }
                }

                How is the first conditional testing to see if both are true? It looks to
                me like the first test is only against charGoggles and not anything else.

                Thanks



                • 5. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
                  kglad Adobe Community Professional & MVP
                  the problem is because of this forum and the difference between users that use the internet and the newsgroup to view this forum. i'm seeing twisted's edited message which has a correction (that i noted in my addendum above).

                  newsgroup users don't see the edited message and smb responded to twisted's message before the correction was made by twisted. i'm pretty sure smb should only see the edited message if he re-reads twisted original post.

                  here's what i saw when i responded and what i still see:

                  • 6. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
                    Level 7
                    Geez.. more lame forum problems. Wonder if they will _ever_ get the forum
                    working properly.

                    --
                    Dave -
                    Adobe Community Expert
                    www.blurredistinction.com
                    http://www.adobe.com/communities/experts/


                    • 7. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
                      twistedpancreas Level 1
                      yeah sorry guys,

                      i saw that mistake at the last minute and changed it from

                      if (charGoggles) {

                      to

                      if (charGoggles && charCoat) {

                      And sorry I should have asked why my code isn't getting past

                      if (charGoggles && charCoat) {
                      gotoAndPlay("correct");

                      if only the goggles have been selected

                      Kglad, when you say "to remedy, remove the prefix "var" that precedes your booleans." won't this cause errors? and do u mean remove the "var" that are in the button's code or the "var" for

                      var charGoggles:Boolean = false;
                      var charCoat:Boolean = false;

                      or should i remove these 2 lines completely (which i tried and didnt work)?

                      thanks for you're help so far guys and sorry to those not using the forum, i wont edit my posts anymore
                      • 9. Re: &quot;if&quot;, &quot;else&quot; and &quot;if else&quot; statements
                        twistedpancreas Level 1
                        ah of course,

                        gee i hate being a noob :D

                        for some reason i thought you had to include the boolean statement when setting the var to the other boolean setting.

                        thanks kglad you're a Lifesaver

                        :D