5 Replies Latest reply on Nov 23, 2010 10:53 AM by Lee Burrows

    Transferring color variable changes color value

    Chris McLaughlin Level 1

      Hi.

       

      I have two variables:

       

      var defaultMainHexidec:Number = 0xFFFFFF;

      var mainHexidec:Number;

       

      if a value isn't assigned to mainHexidec I assign the default value as follows:

       

      mainHexidec = defaultMainHexidec;

      trace("mainHexidec = " + mainHexidec);

       

      The trace returns the same value as the value returned when tracing the defualt value.

       

      But if I use mainHexidec as the fill for a rectangle, the color is black while the default value is white (or any other color).  If I set the color using the default variable the fill appears correctly.  If I set the number using the number traced in either defaultMainHexidec or mainHexidec (it traces the same number) the color is correct.

       

      Does anyone have an idea what's going on?

       

      Best regards,

       

       

      Chris McLaughlin

        • 1. Re: Transferring color variable changes color value
          Lee Burrows Level 4

          can you provide more code - its hard to debug from what you provide

          • 2. Re: Transferring color variable changes color value
            Chris McLaughlin Level 1

            Hi Lee,

             

            Thanks for your message.  Sure I can provide more code.  FYI, I found a work around which was to simply set the mainHexidec variable to the value I was assigning to the defaultMainHexidec variable.  But if you see anything, I'd appreciate knowing where I've gone wrong.

             

            //Here are the lines where the variables are set:

             

            var defaultMainHexidec:Number = 0xFFFFFF;

            var mainHexidec:Number;

             

            //I load values from an xml file:

             

            var xmlLoader:URLLoader = new URLLoader();

            xmlLoader.addEventListener(Event.COMPLETE, loadContent);

            xmlLoader.load(new URLRequest("xml/countdown.xml")); //+ "?" + Math.random()

             

            function loadContent(e:Event):void {

                 //trace("loadContent triggered")

                 XML.ignoreWhitespace = true;

                 counterData = new XML(e.target.data);

             

                 //I check to see if there is an existing color value and if not assign the default;

                 if(counterData.color.mainHexidec.text()==undefined) {

                      mainHexidec = defaultMainHexidec;

                      trace("use the defaultMainHexidec = " + mainHexidec);

                      //this traces the correct value;

                 } else {

                      mainHexidec = Number(counterData.color.mainHexidec.text());

                 }

             

            //I create the shape:

             

            var rectangle:Shape = new Shape;

            rectangle.graphics.beginFill(mainHexidec);//If mainHexidec value is set directly this works

            rectangle.graphics.drawRect(0, 0, 320,107);

            rectangle.graphics.endFill();

            addChild(rectangle);

             

            Thanks,

             

             

            Chris

            • 3. Re: Transferring color variable changes color value
              Lee Burrows Level 4

              cant see anything wrong with the code - unless you are creating the rectangle before loadContent runs?

              • 4. Re: Transferring color variable changes color value
                Chris McLaughlin Level 1

                cant see anything wrong with the code - unless you are creating the rectangle before loadContent runs?

                 

                Doh!

                 

                Well of course I was.

                 

                I encased the shape code in a function and called the function after the color was set and it was all good.

                 

                Thanks Lee!