5 Replies Latest reply on Apr 27, 2010 3:00 PM by geekay2

    Write this code in few lines?

    Ron Colmen Level 2

      Reffer the code below. I've got 120 names (apple, lemon, lime...) and 10 movieclip objects (wood_mc, metal_mc, dust_mc). I need a way to make the code short.

       

      //SET 1

      colorchange = new Color(wood_mc);{
      if (myclip== "APPLE"){
      colorchange.setRGB(0x94372C);
      }
      if (myclip== "LEMON"){
      colorchange.setRGB(0xF4B000);
      }

      //repeated 118 times more...

       

      //SET 2

      colorchange = new Color(metal_mc);{
      if (myclip== "APPLE"){
      colorchange.setRGB(0x94372C);
      }
      if (myclip== "LEMON"){
      colorchange.setRGB(0xF4B000);
      }

      //repeated 118 times more...

       

      this entire code goes in the same frame.

        • 1. Re: Write this code in few lines?
          Ned Murphy Adobe Community Professional & MVP

          Unless you have some kind of array holding the color values, you'll be stuck writing out all 120 sets.  Beyond that, if you understand how looping works you should be able to reason this out yourself.

          1 person found this helpful
          • 2. Re: Write this code in few lines?
            Janaki Lakshmikanthan Adobe Employee

            You can even have these items in an external XML and apply the values in a single loop by calling the right nodes.

             

            Regards,

            Janaki L

            1 person found this helpful
            • 3. Re: Write this code in few lines?
              Ron Colmen Level 2

              Thanks both of you!! 

               

              I was actually expecting an answer like what Janaki has suggested.

              • 4. Re: Write this code in few lines?
                Ron Colmen Level 2

                I'm not successful. can you show a sample code how to write it either way.

                 

                I've got METAL_MC, WOOD_MC, WATER_MC and FIRE_MC. Also 120 colors (NIGHT, LEMON...). a[2] is the color name which is being pulled form a database (there are a[3], a[4],a[5] and a[6]).

                 

                //color change for METAL_MC

                colorchange = new Color(metal_mc);{
                if (a[2]== "NIGHT"){
                colorchange.setRGB(0x000000);
                }

                colorchange = new Color(metal_mc);{
                if (a[2]== "LEMON"){
                colorchange.setRGB(0xC40000);
                }

                //118 colors more

                //color change for WOOD_MC

                colorchange = new Color(wood_mc);{
                if (a[2]== "NIGHT"){
                colorchange.setRGB(0x000000);
                }

                colorchange = new Color(wood_mc);{
                if (a[2]== "LEMON"){
                colorchange.setRGB(0xC40000);
                }

                //118 colors more

                • 5. Re: Write this code in few lines?
                  geekay2
                  var fruitcolor:Object = new Object();
                  fruitcolor.APPLE = 0x94372C;
                  fruitcolor.LEMON = 0xF4B000;
                  //...et al...

                   

                  function setFruitColor(who:MovieClip, colorID:String){
                       if(fruitcolor[colorID]==null){
                            trace(colorID +" is not a valid ColorID!");
                            return false;
                       }
                  
                       var c = new Color(who);
                       c.setRGB(fruitcolor[colorID]);
                       return true;
                  
                  }
                  

                   

                   

                  I am not sure what you mean by SET #, but maybe the above might help.

                   

                   

                  More or less to use, you need to populate fruitcolor with each of your color names and the color numbers.

                   

                  Then to use the function, you simply do the following:

                   

                  setFruitColor(wood_mc, "APPLE");
                  
                  
                  //or check if a color was successfully added:
                  if ( setFruitColor(wood_mc, "BROWN") ){
                       trace("Color was added Successfully!!!!");
                  }else{
                       trace("Woopsie Doodles, you seem to have encountered an error!");
                  }
                  
                  

                   

                   

                   

                   

                   

                  Hope this helps!

                   

                  -GK