4 Replies Latest reply on May 1, 2007 5:12 AM by Newsgroup_User

    Help: Casting String gotten from Xml to a Number Type

    kevmann16
      Hi all,

      I have a major problem, I have parsed the data from an xml doc to global vars in flash and need to use a few of them as numbers for things the position of "nodes" on the screen. However I can not cast the data stored as a var to a number or int value.

      For example in the code below when the set Node button is pressed it traces the "Node's" current X value, then the value in the global var gotten from the xml, which contains the x position of where to place the Node.

      Next it creates a new var of Number Type and assigns it the Number value of the string in global var.

      It then traces the new value of SNtopLeftX and sets the x pos of the Node's movie clip to the value of SNtopLeftX

      However the Node doesn't move and this is the output I get from the traces

      Start Node is at x = -734.5
      nod_topLeftX is 16
      TopLeftX = 16
      SN TopLeftX = NaN

      I do not understand why I am getting NaN when the string of nod_topLefX is 16, any ides?
      ------------------------------------------------------------------------------------------ -------------------------
        • 1. Re: Help: Casting String gotten from Xml to a Number Type
          Level 7

          Hi,

          you got some casing mixed up:

          nod_topLeftX
          Number(nod_TopLeftX);

          lower "t" one time, upper the other.

          Manno

          kevmann16 wrote:
          > Hi all,
          >
          > I have a major problem, I have parsed the data from an xml doc to global vars
          > in flash and need to use a few of them as numbers for things the position of
          > "nodes" on the screen. However I can not cast the data stored as a var to a
          > number or int value.
          >
          > For example in the code below when the set Node button is pressed it traces
          > the "Node's" current X value, then the value in the global var gotten from the
          > xml, which contains the x position of where to place the Node.
          >
          > Next it creates a new var of Number Type and assigns it the Number value of
          > the string in global var.
          >
          > It then traces the new value of SNtopLeftX and sets the x pos of the Node's
          > movie clip to the value of SNtopLeftX
          >
          > However the Node doesn't move and this is the output I get from the traces
          >
          > Start Node is at x = -734.5
          > nod_topLeftX is 16
          > TopLeftX = 16
          > SN TopLeftX = NaN
          >
          > I do not understand why I am getting NaN when the string of nod_topLefX is 16,
          > any ides?
          >
          > --------------------------------------------------------------------------------
          > -----------------------------------
          >
          > butNodeSet.onRelease = function() {
          >
          > trace ("Start Node is at x = " + mcStartNode._x);
          > trace ("nod_topLeftX is " + nod_topLeftX);
          >
          > var SNtopLeftX:Number = Number(nod_TopLeftX);
          > trace ("StartNode TopLeftX = " + SNtopLeftX);
          > mcStartNode._x = SNtopLeftX;
          > }
          >

          --
          ----------
          Manno Bult
          http://www.aloft.nl
          manno@xs4all.nl
          • 2. Re: Help: Casting String gotten from Xml to a Number Type
            kevmann16 Level 1
            Hi,

            Thank you for your help but it made no difference. Even if I use the line:

            _global.nod_topLeftX = Number(xmlDoc_xml.firstChild.childNodes[3].childNodes[0].childNodes[3].childNodes[0]);

            So it selects the number value as I import it I get Is NaN if I trace nod_topLeftX
            • 3. Re: Help: Casting String gotten from Xml to a Number   Type
              Level 7

              A small adaptation (code below) from your first example
              * corrected typo in Number() parameter
              * no xml
              * movieClip called mcStartNode on stage

              does what I would expect. Sure there's no other typo's or scope issues?

              function test() {

              nod_topLeftX = "0";

              trace ("Start Node is at x = " + mcStartNode._x);
              trace ("nod_topLeftX is " + nod_topLeftX + "; type: " + typeof
              nod_topLeftX );
              trace(nod_topLeftX + " to Number renders " + nod_topLeftX);

              var SNtopLeftX:Number = Number(nod_topLeftX);
              trace ("StartNode TopLeftX = " + SNtopLeftX);
              mcStartNode._x = SNtopLeftX;
              }
              test();




              kevmann16 wrote:
              > Hi,
              >
              > Thank you for your help but it made no difference. Even if I use the line:
              >
              > _global.nod_topLeftX =
              > Number(xmlDoc_xml.firstChild.childNodes[3].childNodes[0].childNodes[3].childNode
              > s[0]);
              >
              > So it selects the number value as I import it I get Is NaN if I trace
              > nod_topLeftX
              >

              --
              ----------
              Manno Bult
              http://www.aloft.nl
              manno@xs4all.nl
              • 4. Re: Help: Casting String gotten from Xml to a Number Type
                kevmann16 Level 1
                I've managed to gwet it working with help, I can force it to an integer using:
                _global.nod_topLeftX = parseInt(xmlDoc_xml.firstChild.childNodes[3].childNodes[0].childNodes[3].childNodes[0]);