4 Replies Latest reply on Dec 5, 2006 1:46 AM by jonnybennett

    for, else. default to else

    jonnybennett
      The following code seems to default to the else statement, even if the if statement is successful...

      However if you take out the else statement the code works fine and when the if statement(the hittest) is successfull the code is implemented. However why when you add the else stament is it defaulting to the else function even if the hittest is successful? Thanks to any responders.
        • 1. Re: for, else. default to else
          Level 7
          jonnybennett,

          > However if you take out the else statement the code works
          > fine and when the if statement(the hittest) is successfull the
          > code is implemented. However why when you add the else
          > stament is it defaulting to the else function even if the hittest
          > is successful?

          There's a lot going on in that sample code. Often, when something isn't
          clear -- or worse, is confusing -- it helps to reduce the complexity of the
          problem in order to troubleshoot. For example, are you absolutely certain
          the hitTest() result is behaving as expected? Have you tried a trace()
          statement on either side of the else clause to prove in no uncertain terms
          when you're getting true and when you're getting false?


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: for, else. default to else
            jonnybennett Level 1
            Thanks for the response David, you were right to test using the trace, but sadly i can't seem to resolve the problem... I have tried to run it with a

            if (this.hitTest(orderByTimeline['orderBy'+d])) {
            ifText.text='yes IF has run';
            }
            else{
            elseText.text='yes ELSE has run';}

            the same ideas as a trace statement.... the results were that whether or not the hitTest was successfull both operations are being run. Also should be noted that if you take out the else staement the if stement also runs wheteher or not the hitTest is successful.

            I assume the problem is arising with the fact that it is on a loop within a loop. Any ideas? thanks J.
            • 3. Re: for, else. default to else
              Level 7
              jonnybennett,

              > the results were that whether or not the hitTest was successfull
              > both operations are being run.

              I can honestly say I've never seen that before. I mean, something is
              either true in ActionScript, or it isn't. ;) Is it possible that your
              issue is one of timing? You're in a for() loop that repeats 8 times ...
              maybe what appears to be an if() statement "playing it both ways" is
              actually that same if() statement making discrete evaluations in rapid
              sequential order.

              In addition to tracing (or sending to a text field) an expository
              string, how about tracing the value of getTimer(), while you're at it?

              > Also should be noted that if you take out the else staement the if
              > stement also runs wheteher or not the hitTest is successful.

              So if *none* of your orderByTimeline.orderBy1, orderByTimeline.orderBy2,
              etc., movie clips collides with "this" -- ... hold on.

              Actually, what are you comparing, here? I don't know how many times
              your p variable iterates, or what its range is, but it's not unlikely that
              you're often comparing the same object against itself; that is, testing if
              orderByTimeline.orderBy1 collides with orderByTimeline.orderBy1. I don't
              know that I've ever tried to evaluate a movie clip's hitTest() against
              itself. Does that shed some light on anything?

              > I assume the problem is arising with the fact that it is on a loop
              > within a loop. Any ideas?

              It might be very well be, man. Trace out the MovieClip._name property
              of each clip as it makes the hitTest() -- that could be useful (both the
              tester and the testee). I have a hunch you'll see you're making a number of
              repeat collision tests that you can avoid. In the very least, it should
              reduce unnecessary cycles. Troubleshooting is fundamentally about giving
              yourself more data to work with ... peeking under the blanket, so to speak.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: for, else. default to else
                jonnybennett Level 1
                I've just been able to log back into this forum, for some reason its not been loading up for me. Anyway I have been able to solve the problem... just... my brain somewhat hurts!. Anyway I see that you have also spotted the problem and that was that the hit test was looping through and including itself in the hittest, therefore was always resulting in a positive hit.
                However that wasonly the beginning of the looping nightmare... anyway the folowing code works, it is not pretty and and im sure could be changed to do the same thing in only a few lines of code... anyway thanks for your support david, heres the code if anyone is interested....


                for (p=1;p<=9;p++){
                {orderByTimeline['orderBy'+p].numText.text=p;
                orderByTimeline['orderBy'+p].posText.text=p;}

                orderByTimeline['orderBy'+p].onPress=function(){
                startDrag(this,true);
                origOrderByX=this._x;
                origOrderByY=this._y;
                origPos=this.posText.text;
                this._xscale=30;
                this._yscale=30;
                this.swapDepths(_global.nIndex++);}

                orderByTimeline['orderBy'+p].onRelease=orderByTimeline['orderBy'+p].onReleaseOutside=funct ion(){
                for (d=1;d<=9;d++){
                stopDrag();

                if (this.hitTest(orderByTimeline['orderBy'+d])) {

                if (orderByTimeline['orderBy'+d].numText.text!=this.numText.text){
                thereWasAHit=1;
                //trace (orderByTimeline['orderBy'+d].numText.text + this.numText.text);
                //trace (orderByTimeline['orderBy'+d].numText.text + 'swap places with that number.');
                hitX=orderByTimeline['orderBy'+d]._x;
                hitY=orderByTimeline['orderBy'+d]._y;
                hitPos=orderByTimeline['orderBy'+d].posText.text;
                orderByTimeline['orderBy'+d]._x=origOrderByX;
                orderByTimeline['orderBy'+d]._y=origOrderByY;
                orderByTimeline['orderBy'+d].posText.text=origPos;
                this._x=hitX;
                this._y=hitY;
                this.posText.text=hitPos;
                this._xscale=100;
                this._yscale=100;
                ifText.text=orderByTimeline['orderBy'+d].numText.text+'and d'+this.numText.text;
                }}}

                for (a=1;a<=9;a++){
                if (this.numText.text==orderByTimeline['orderBy'+a].numText.text&&thereWasAHit!=1)
                {
                //trace (orderByTimeline['orderBy'+a].numText.text + 'return to that space.');
                this._x=origOrderByX;
                this._y=origOrderByY;
                this._xscale=100;
                this._yscale=100;
                }}

                delete thereWasAHit;
                }}