12 Replies Latest reply on Aug 24, 2007 12:37 PM by Greg Dove

    dynamic text color

    SClukey
      I am trying to use an xml document to dynamically load the text color into my movie, and i can get the color, but when I try to use that value to change the text color, it doesn't do anything, how can I get this to work?

      Thanks

      This is my code:

        • 1. Re: dynamic text color
          edmundep2
          try making a new TextFormat and then applying that..

          var tf:TextFormat = new TextFormat();
          tf.color = txtColor; // you might need to cast txtColor as a Number tho.. Number(txtColor)
          ttle.setTextFormat(tf);
          • 2. Re: dynamic text color
            SClukey Level 1
            Casting it as a number return NaN because of the x and Fs, and I tried this but it's doing the same thing

            • 3. Re: dynamic text color
              NSurveyor Level 2
              In general, my_txt.textColor = "0xFFFFFF" or my_txt.textColor = 0xFFFFFF works, so that shouldn't be the issue. This draws me to two possible problems:

              1. ttle isn't a textfield or a proper path to one. To verify, add trace(ttle) in your code, or try: ttle.textColor = "0xFFFFFF";

              2. txtColor has some trailing characters on the side. To check, add trace('['+txtColor+']'); and make sure that you see [0xFF0000].
              • 4. Re: dynamic text color
                SClukey Level 1
                I have tried ttle.textColor = 0xFFFFFF; and it works, but ttle.textColor = "0xFFFFFF"; errors and says that it was expecting a number not a string, and I did trace("["+txtColor+"]"); and the output was [0xFFFFFF].

                Also, I tried

                var txtColor = 0xFFFFFF;
                ttle.textColor = txtColor;

                and it worked, so could it have anthing to do with the way the XPath function is returning, and even though I put :Number could it still be reading it as a string, and if so, how can I change it's format without using Number(txtColor); because that returns NaN?

                Thanks
                • 5. Re: dynamic text color
                  NSurveyor Level 2
                  Are you using CS3 or Flash 8? In Flash 8, using a string works, and so does casting as a Number.
                  • 6. Re: dynamic text color
                    Greg Dove Level 4
                    There are two issues here I think.
                    1. Getting the correct value.
                    var txtColor:Number = XPathAPI.selectSingleNode(this.firstChild, path+"/textColor/*");

                    This probably gives you a correct trace (if the xpath is correct for what you want to do) but the trace is misleading.
                    trace applies the toString method to its arguments (I believe). And putting the :Number there doesn't make it a number, the compiler just treats it like one.

                    What you actually have is this:
                    var txtColor:XMLNode = XPathAPI.selectSingleNode(this.firstChild, path+"/textColor/*");

                    and if
                    trace(txtColor.nodeType)
                    shows '3' then you have a textNode which contains the value you're after.

                    So, try this:
                    var txtColor:Number = Number((XPathAPI.selectSingleNode(this.firstChild, path+"/textColor/*").nodeValue);


                    This *should* get you the value you need.

                    2. Assigning the value

                    If the above works, then assigning the number should be fine...but just to clarify the other points....
                    Regardless of whether its flash 8 or flash CS3 the compiler will give an error with the string assignment to a textfield's textColor property if your textfield has been declared as a TextField.

                    E.g.
                    This fails:
                    var testtext:TextField;
                    testtext.textColor ="0xff00ff";

                    But this works without the declaration:

                    testtext.textColor ="0xff00ff";

                    If you're getting that error then you need to use Number(stringValue) and it will work.
                    • 7. Re: dynamic text color
                      NSurveyor Level 2
                      I can't believe I overlooked both of those!
                      • 8. Re: dynamic text color
                        Greg Dove Level 4
                        Yeah.. that trace result with XML textNodes has had me stumped before... its a real frustration trap, because the trace tells you that you have the 'right' value but it causes errors when you convert to Number. It crops up occasionally here in the forums and I always check for it when I look at XML problems.

                        The other thing about textColor I didn't know - I had to check that. liveDocs lists the property as expecting a Number type which is what the compiler checks against it you're assigning it to a 'known' (i.e. declared) textfeld object. I guess that internally the native code in the textfield applies a Number() conversion to whatever it receives as well which is why the string version works - if it can get past the compiler.



                        • 9. Re: dynamic text color
                          SClukey Level 1
                          quote:

                          Originally posted by: GWD
                          So, try this:
                          var txtColor:Number = Number(XPathAPI.selectSingleNode(this.firstChild, path+"/textColor/*").nodeValue);

                          This *should* get you the value you need.



                          Thanks for the help, that fixed it, just another somewhat related question, what system does flash use to get 16777215 as white?

                          Thanks

                          • 10. Re: dynamic text color
                            Greg Dove Level 4
                            each of red, green and blue is represented by 256 possible values (from 0 to 255) or 00 to FF in hex. Basically one byte each. String them together and you can represent them as two digits each in hex or convert to a decimal integer the following way.

                            white = 0xFFFFFF

                            R = FF = 255 x 65536 = 16711680
                            G = FF = 255 x 256 = 65280
                            B = FF = 255 x 1 = 255

                            16711680+65280+255=16777215
                            • 11. dynamic text color
                              SClukey Level 1
                              Cool, well thanks again
                              • 12. Re: dynamic text color
                                Greg Dove Level 4
                                You're welcome.