2 Replies Latest reply on Feb 29, 2012 5:07 PM by subtlefly72

    how stupid is this code?

    subtlefly72

      Hey I am really trying to improve my code writing- see my last post on for and arrays, but I have written this... its a bit embarrassing how stupid it is, but it works good and I understand how it works, can I use it?   It is for a drag and drop exercise where there is a buttons that checks answers and then two dynamci text boxes that give responses.

      Cheers

      sub

       

      ans1.ans1text.text="beans";

      ans2.ans1text.text="chicken";

      ans3.ans1text.text="rice";

      ans4.ans1text.text="rocks";

      ans5.ans1text.text="dirt";

      ans6.ans1text.text="poo";

      ans7.ans1text.text="dog";

      var incorrect:Number=0;

       

       

      var correct:Number=0;

       

       

      trace(getRect(target1));

       

       

       

       

      var rightanswers:Array = ["ans1","ans2","ans3"];

      var useranswers:Array= [];

       

       

       

       

       

       

       

       

      this.ans1.addEventListener(MouseEvent.MOUSE_DOWN, pickMe1);

      this.ans1.addEventListener(MouseEvent.MOUSE_UP,dropMe1);

      function pickMe1(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe1(event:MouseEvent):void {

          event.target.stopDrag();

       

      }

       

       

      this.ans1.buttonMode = true;

      this.ans2.addEventListener(MouseEvent.MOUSE_DOWN, pickMe2);

      this.ans2.addEventListener(MouseEvent.MOUSE_UP,dropMe2);

      function pickMe2(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe2(event:MouseEvent):void {

          event.target.stopDrag();

       

       

      }

       

       

      this.ans2.buttonMode = true;

      this.ans3.addEventListener(MouseEvent.MOUSE_DOWN, pickMe3);

      this.ans3.addEventListener(MouseEvent.MOUSE_UP,dropMe3);

      function pickMe3(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe3(event:MouseEvent):void {

          event.target.stopDrag();

       

       

       

       

       

       

       

       

       

       

      }

       

       

      this.ans3.buttonMode = true;

      this.ans4.addEventListener(MouseEvent.MOUSE_DOWN, pickMe4);

      this.ans4.addEventListener(MouseEvent.MOUSE_UP,dropMe4);

      function pickMe4(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe4(event:MouseEvent):void {

          event.target.stopDrag();

       

       

       

       

       

      }

       

       

      this.ans4.buttonMode = true;

      this.ans5.addEventListener(MouseEvent.MOUSE_DOWN, pickMe5);

      this.ans5.addEventListener(MouseEvent.MOUSE_UP,dropMe5);

      function pickMe5(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe5(event:MouseEvent):void {

          event.target.stopDrag();

       

      }

       

       

      this.ans5.buttonMode = true;

      this.ans6.addEventListener(MouseEvent.MOUSE_DOWN, pickMe6);

      this.ans6.addEventListener(MouseEvent.MOUSE_UP,dropMe6);

      function pickMe6(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe6(event:MouseEvent):void {

          event.target.stopDrag();

       

       

       

      }

       

       

      this.ans6.buttonMode = true;

      this.ans7.addEventListener(MouseEvent.MOUSE_DOWN, pickMe7);

      this.ans7.addEventListener(MouseEvent.MOUSE_UP,dropMe7);

      function pickMe7(event:MouseEvent):void {

          event.target.startDrag();

       

      }

      function dropMe7(event:MouseEvent):void {

          event.target.stopDrag();

       

      }

       

       

      this.ans7.buttonMode = true;

       

       

       

       

       

       

       

       

      checker.addEventListener(MouseEvent.CLICK, checker1);

      function checker1(event:MouseEvent){

                trace("Checker");

                right.text="";

                wrong.text="";

                useranswers=[];

                checkans1();

                checkans2();

                checkans3();

                checkans4();

                checkans5();

                checkans6();

                checkans7();

                var check1:Number=(useranswers.indexOf("ans1")+1);

                var check2:Number=(useranswers.indexOf("ans2")+1);

                var check3:Number=(useranswers.indexOf("ans3")+1);

                var checklength:Number=useranswers.length;

                var combined:Number=(check1+check2+check3)

                rightans();

                checkwrong();

                function checkwrong(){

                if (combined<=0){

                          trace("all wrong")

                          wrong.text="all of your answers are wrong";

                }

      }

                 function rightans(){

                          if(combined==6&&checklength==3){

                          trace("correct");

                          right.text="Correct";

                          }else if(check1>0 && check2>0 && check3>0){

                          trace("you are correct but..");

                right.text="you are correct but..";

       

                }else if(check1>0&&check2>0||check3>0&&check2>0||check1>0&&check3>0){

                          trace("you have two correct answers");

                          right.text="you have two correct answers";

                }else if(check1>0||check2>0||check3>0){

                          trace("you have one correct answer");

                          right.text="you have one correct answer";

                }

      }

       

       

       

                if(combined==6 && checklength==4){

                          trace("you have one wrong answer picked");

                          wrong.text="you have one wrong answer picked";

                }else if(combined==6 && checklength==5){

                          trace("you have two wrong answers picked");

                          wrong.text="you have two wrong answers picked";

                }else if(combined==6 && checklength==6){

                          trace("you have three wrong answers picked");

                          wrong.text="you have three wrong answers picked";

                }else if(combined==6 && checklength==7){

                          trace("you have four wrong answers picked");

                          wrong.text="you have four wrong answers picked";

                }

       

      }

      function checkans1(){

       

      if ((ans1.hitTestObject(target1)==true)){

             

         useranswers.push("ans1"); 

        

         trace(useranswers);

      }

      }

      function checkans2(){

       

      if ((ans2.hitTestObject(target1)==true)){

             

         useranswers.push("ans2"); 

        

         trace(useranswers);

      }

      }

      function checkans3(){

       

      if ((ans3.hitTestObject(target1)==true)){

             

         useranswers.push("ans3"); 

        

         trace(useranswers);

      }

      }

      function checkans4(){

       

      if ((ans4.hitTestObject(target1)==true)){

             

         useranswers.push("ans4"); 

        

         trace(useranswers);

      }

      }

      function checkans5(){

       

      if ((ans5.hitTestObject(target1)==true)){

             

         useranswers.push("ans5"); 

        

         trace(useranswers);

      }

      }

      function checkans6(){

       

      if ((ans6.hitTestObject(target1)==true)){

             

         useranswers.push("ans6"); 

        

         trace(useranswers);

      }

      }

      function checkans7(){

       

      if ((ans7.hitTestObject(target1)==true)){

             

         useranswers.push("ans7"); 

        

         trace(useranswers);

      }

      }

        • 1. Re: how stupid is this code?
          Ned Murphy Adobe Community Professional & MVP

          There are a number of ways you could improve that code.  Here are a few..

           

          1) Use of "this" the way you have used it is unnecessary.

           

          2) All nut one set of your pickMe and dropMe functions are unnecessary and one of any one of them could be shared by all the ans objects

           

          3) when using a boolean in a conditional test it is unnecessqary to use == true or == false.  A conditionalk evaluates the boolean result oif whatever it contains, so you only need the part that generates the boolean result *ex:  if(ans7.hitTestObject(target1)){...

           

          4) Your evaluations at the end could probably all be reduce to testing within the dropMe function by assigning a value to each ans object (1,2,3...)

          1 person found this helpful
          • 2. Re: how stupid is this code?
            subtlefly72 Level 1

            Thanks Ned, I will look into all of those suggestions, the reason why I didnt put the evaluation into the drop function (it was there at the start) is that if you drop a correct answer on the target, then picked it up and dropped it on the target again, it messed up all the calc!  Like I said, I know this code is ugly, but it can't be broken.  The user can do anything and the code will still give them the right response.  I will try to improve it with your suggestions.

            Cheers

            sub