3 Replies Latest reply on Oct 11, 2008 10:05 AM by Rothrock

    onEnterFrame with multiple comparisons problem

    cmarlo
      Hi Everyone,

      Hope someone can help. I have a simple script below that is giving me some headaches. I have two movie clips(square1 and square2) that are draggable. When they are dragged over the movie "circle," I want the polygon movie clip to be visible. The problem is the onEnterFrame only reads the last comparison for the hitTest on square2, so that square1 won't turn on polygon. Thanks to any advice.

      square1.onPress = function () {
      this.startDrag();
      }

      square1.onRelease = function () {
      stopDrag();
      }

      square2.onPress = function () {
      this.startDrag();
      }

      square2.onRelease = function () {
      stopDrag();
      }

      onEnterFrame = function () {

      if (square1.hitTest(circle) == true) {
      holder.polygon._visible = true;
      } else {
      holder.polygon._visible = false;
      }

      if (square2.hitTest(circle) == true) {
      holder.polygon._visible = true;
      } else {
      holder.polygon._visible = false;
      }

      }

        • 1. Re: onEnterFrame with multiple comparisons problem
          robdillon Most Valuable Participant
          Your two if statements in the enterframe function are competing with each other. One says true, the other false, The second one always wins. Just change the function to look like this:
          • 2. Re: onEnterFrame with multiple comparisons problem
            cmarlo Level 1
            That makes sense. I'll try working this concept into my real application.
            Thanks for the help Rob. Your the best.

            • 3. Re: onEnterFrame with multiple comparisons problem
              Rothrock Level 5
              If I'm understanding then you could use OR, which is made with || (that is the pipe key just above the return on a US keyboard).

              if(square1.hitTest(circle) || square2.hitTest(circle)){
              holder.polygon._visible=true;
              } else {
              holder.polygon._visible=false;
              }

              To explain the simplifications here. hitTest returns true or false, so you don't actually need the comparison. The way the parens in if() work is that if what is inside comes out to true it is true and false is false. (More or less). So there are 4 ways this could be:

              if(false || false);
              if(true || false);
              if(false || true);
              if(true || true);

              Remember that || means "OR". So in this case the first one is false and the rest are true.