5 Replies Latest reply on Dec 17, 2012 12:22 PM by Ned Murphy

    Array value Equal to string (if statement)

    LukeElChul Level 1

      Im trying to make this code shorter. I tried to make an array which Could be wslot1, wslot2 and wslot3. The weapon Index is the value sent to the function when the slot is clicked in the game. so if you click slot1,  useWeapon(1) would be called.

       

       

       

      var wslot1:String = "";

      var wslot2:String = "";

      var wslot3:String = "";

       

      var wslot1full:Boolean = false;

      var wslot2full:Boolean = false;

      var wslot3full:Boolean = false;

       

      var weapon:String = "";

       

      var Wslots:Array = new Array(wslot1, wslot2, wslot3);

       

       

      function useWeapon(weaponIndex:Number) {

                if (_root.Wslots[weaponIndex] == "sword" && weaponIndex == 1) {

                          _root.wslot1full = false;

                          _root.wslot1 = "Nothing";

                          _root.cam.character.ws1.gotoAndStop(1);

                          _root.mc.weapon.gotoAndStop(2);

                          _root.weapon = "sword";

                }else if (_root.wslot1 == "baculo" && weaponIndex == 1) {

                          _root.wslot1full = false;

                          _root.wslot1 = "Nothing";

                          _root.mc.weapon.gotoAndStop(3);

                          _root.weapon = "baculo";

                          _root.cam.character.ws1.gotoAndStop(1);

                }

                if (_root.wslot2 == "sword" && weaponIndex == 2) {

                          _root.wslot2full = false;

                          _root.wslot2 = "Nothing";

                          _root.cam.characer.ws2.gotoAndStop(1);

                          _root.HitOrShoot = "Hit";

                          _root.mc.weapon.gotoAndStop(2);

                          _root.weapon = "sword";

                }else if (_root.wslot2 == "baculo" && weaponIndex == 2) {

                          _root.wslot2full = false;

                          _root.wslot2 = "Nothing";

                          _root.mc.weapon.gotoAndStop(3);

                          _root.weapon = "baculo";

                          _root.cam.character.ws2.gotoAndStop(1);

                }

                if (_root.wslot3 == "sword" && weaponIndex == 3) {

                          _root.wslot3full = false;

                          _root.wslot3 = "Nothing";

                          _root.mc.weapon.gotoAndStop(2);

                          _root.weapon = "sword";

                          _root.cam.characer.ws3.gotoAndStop(1);

                }else if (_root.wslot3 == "baculo" && weaponIndex == 3) {

                          _root.wslot3full = false;

                          _root.wslot3 = "Nothing";

                          _root.mc.weapon.gotoAndStop(3);

                          _root.weapon = "baculo";

                          _root.cam.character.ws3.gotoAndStop(1);

                }

      }

       

       

      Thanks for anyone who can help me with this

        • 1. Re: Array value Equal to string (if statement)
          Ned Murphy Adobe Community Professional & MVP

          In general, you can often use looping and bracket notation to reduce code that is repetitive.  In your case that applies to some degree, though the first conditional (if) of each group varies from one to the next which makes it a bit more difficult to whittle down to a generic solution that can handle all with the same code.  The second half of the conditionals (else if) appears to be nearly identical for all three sets so that could be managed fairly easily.

           

          So you need to try to get the first half of your conditionals to be similar for all, or otherwise you need to build in some specific conditionals within them to deal with logic specific to certain wslot# values

          1 person found this helpful
          • 2. Re: Array value Equal to string (if statement)
            LukeElChul Level 1

            I simplified it down to this

             

            function useWeapon(weaponIndex:Number, weaponSelected:String, Type:String, Frame:Number) {

                      if (_root.wslot1 == weaponSelected && weaponIndex == 1) {

                                _root.wslot1full = false;

                                _root.wslot1 = "Nothing";

                                _root.cam.character.ws1.gotoAndStop(1);

                                _root.HitOrShoot = Type;

                                _root.mc.weapon.gotoAndStop(Frame);

                                _root.weapon = weaponSelected;

                      }

                      if (_root.wslot2 == weaponSelected && weaponIndex == 2) {

                                _root.wslot2full = false;

                                _root.wslot2 = "Nothing";

                                _root.cam.character.ws2.gotoAndStop(1);

                                _root.HitOrShoot = Type;

                                _root.mc.weapon.gotoAndStop(Frame);

                                _root.weapon = weaponSelected;

                      }

                      if (_root.wslot3 == weaponSelected && weaponIndex == 3) {

                                _root.wslot3full = false;

                                _root.wslot3 = "Nothing";

                                _root.cam.character.ws3.gotoAndStop(1);

                                _root.HitOrShoot = Type;

                                _root.mc.weapon.gotoAndStop(Frame);

                                _root.weapon = weaponSelected;

                      }

            }

             

            but is there away to change _root.wslot3 to somthing like wslots[3] or wslots[index] so I can then put all the 3 statements into 1

            • 3. Re: Array value Equal to string (if statement)
              Ned Murphy Adobe Community Professional & MVP

              If the three conditional are as similar now as I think I see them to be, then you can reduce that function to be...

               

              function useWeapon(weaponIndex:Number, weaponSelected:String, Type:String, Frame:Number) {

                        for(i=0; i<Wslots.length; i++){

                              if (Wslots[i] == weaponSelected && weaponIndex == i+1) {
                                   _root["wslot"+ String(i+1) +"full"] = false;
                                   Wslots[i] = "Nothing";
                                   _root.cam.character["ws"+ String(i+1)].gotoAndStop(1);
                                   _root.HitOrShoot = Type;
                                   _root.mc.weapon.gotoAndStop(Frame);
                                   _root.weapon = weaponSelected;

                              }
                        }

              }

               

              One thing you should be mindful of is avoiding the unnecessary use of "_root" references in your code.  If the variables/objects you are targeting are in the _root timeline, then there is no need to use _root references, and doing so can turn into a problem later on if this file ever gets loaded into another file.   The use of the _root reference shown in blue for the term using the bracket notation is needed because you need some object reference to precede the brackets... in this case though, using "this" could probably work just as well.

              • 4. Re: Array value Equal to string (if statement)
                LukeElChul Level 1

                Ok thanks for thatb think I have it working