5 Replies Latest reply on Jul 11, 2009 1:55 PM by kglad

    simplify with array loops?

    christoon_ca

       


       

      Hey all


      I have a test here which is for a more complex interaction, but the principle is the same. What I would like to know is how to make the code more efficient, possibly through array loops?

       

      Any help is appreciated

      Thanks

        • 1. Re: simplify with array loops?
          kglad Adobe Community Professional & MVP

          your chances of getting help are diminished if files need to be downloaded.

          • 2. Re: simplify with array loops?
            christoon_ca Level 1

            Oh, sorry. The premise is you click an object on the right, then click an object on the left to match. Then you click a "check" box and if the scores add up correctly you win.

             

            Here's the code

            Thanks

             

            stop();

            // right //

            var r1Var = 0;
            var score_r1 = 0;
            r1.onRelease = function() {
                if (r1Var == 0) {
                    r1.gotoAndStop(2);
                    r1Var = 1;
                } else if (r1Var == 1) {
                    r1.gotoAndStop(1);
                    r1Var = 0;
                } else if (r1Var == 2) {
                    r1a._x = 684;
                    r1a._y = 180;
                    r1.gotoAndStop(1);
                    r1Var = 0;
                    score_r1 = 0;
                }
            };

            var r2Var = 0;
            var score_r2 = 0;
            r2.onRelease = function() {
                if (r2Var == 0) {
                    r2.gotoAndStop(2);
                    r2Var = 1;
                } else if (r2Var == 1) {
                    r2.gotoAndStop(1);
                    r2Var = 0;
                } else if (r2Var == 2) {
                    r2a._x = 684;
                    r2a._y = 207;
                    r2.gotoAndStop(1);
                    r2Var = 0;
                    score_r2 = 0;
                }
            };

            var r3Var = 0;
            var score_r3 = 0;
            r3.onRelease = function() {
                if (r3Var == 0) {
                    r3.gotoAndStop(2);
                    r3Var = 1;
                } else if (r3Var == 1) {
                    r3.gotoAndStop(1);
                    r3Var = 0;
                } else if (r3Var == 2) {
                    r3a._x = 684;
                    r3a._y = 234;
                    r3.gotoAndStop(1);
                    r3Var = 0;
                    score_r3 = 0;
                }
            };

            // left //

            l1.onRelease = function() {
                if (r1Var == 1) {
                    r1a._x = this._x + 60;
                    r1a._y = this._y
                    r1Var = 2;
                } else if (r2Var == 1) {
                    if (score_r2 == 0) {
                        score_r2 += 1
                    }
                    r2a._x = this._x + 60;
                    r2a._y = this._y
                    r2Var = 2;
                } else if (r3Var == 1) {
                    r3a._x = this._x + 60;
                    r3a._y = this._y
                    r3Var = 2;
                }
            };

            l2.onRelease = function() {
                if (r1Var == 1) {
                    r1a._x = this._x + 60;
                    r1a._y = this._y
                    r1Var = 2;
                } else if (r2Var == 1) {
                    r2a._x = this._x + 60;
                    r2a._y = this._y
                    r2Var = 2;
                } else if (r3Var == 1) {
                    if (score_r3 == 0) {
                        score_r3 += 1
                    }
                    r3a._x = this._x + 60;
                    r3a._y = this._y
                    r3Var = 2;
                }
            };

            l3.onRelease = function() {
                if (r1Var == 1) {
                    if (score_r1 == 0) {
                        score_r1 += 1
                    }
                    r1a._x = this._x + 60;
                    r1a._y = this._y
                    r1Var = 2;
                } else if (r2Var == 1) {
                    r2a._x = this._x + 60;
                    r2a._y = this._y
                    r2Var = 2;
                } else if (r3Var == 1) {
                    r3a._x = this._x + 60;
                    r3a._y = this._y
                    r3Var = 2;
                }
            };

            check1.onRelease = function() {
                if (score_r1 + score_r2 + score_r3 == 3) {
                    _root.gotoAndStop(2);
                } else {
                    _root.gotoAndStop(3);
                }
            };

            TA.onRelease = function() {
                (_root.gotoAndStop(1));
            };
            • 3. Re: simplify with array loops?
              kglad Adobe Community Professional & MVP
              try:

              var tl:MovieClip=this;

              for(var i=1;i<=3;i++){
              tl["r"+i+"Var"] = 0;
              tl["score_r"+i] = 0;
              tl["r"+i].rvar=tl["r"+i+"Var"];
              tl["r"+i].rscore = tl["score_r"+i];
              tl["r"+i].ra = tl["r"+i+"a"];

              tl["r"+i].onRelease = function() {
                  if (this.rvar == 0) {
                      this.gotoAndStop(2);
                      this.rvar = 1;
                  } else if (this.rvar == 1) {
                      this.gotoAndStop(1);
                      this.rvar = 0;
                  } else if (this.rvar == 2) {
                      this.ra._x = 684;
                      this.ra._y = 180;
                      this.gotoAndStop(1);
                      this.rvar = 0;
                      this.rscore = 0;
                  }
              };
              }

              similarly for l1,l2,l3
              • 4. Re: simplify with array loops?
                christoon_ca Level 1

                ....................................................wow.

                 

                Ok, I'll try that.

                 

                Thank you very much

                • 5. Re: simplify with array loops?
                  kglad Adobe Community Professional & MVP

                  you're welcome.