3 Replies Latest reply on Aug 16, 2007 10:30 PM by Greg Dove

    Not recognizing variable as number

    jjulie
      Hi

      let's see if I can explain what I'm trying to do, and what the problem is.

      Basically I'm telling a symbol to move from its current position to another. But instead of putting a number behind symbol._x = I placed a variable. For some reason, the symbol won't move to the x coordinate number that is stored inside the variable.
      WHY NOT??? I've used a trace on the variable which reads out a number and I also have replaced the variable behind the _x with a regular number in wich case the symbol moves...

      More Background and code:
      I have an XML file that stores the x and y coordinates for the different items in the database.
      All the information of the XML file gets stored in an Array,
      Sample XML:
      <person>
      <name>Fred</name>
      <profession>Baker</profession>
      <xcoordinate>150</xcoordinate>
      <ycoordinate>25</ycoordinate>
      </person>
      <person>
      ...

      The Array looks like this:
      var aPerson:Array = new Array();
      var xPersonData:XML = new XML();
      xPersonData.ignoreWhite = true;
      xPersonData.load("People.xml");
      xPersonData.onLoad = function(success:Boolean) {
      if (success) {
      var numPeople:Number = xPersonData.firstChild.childNodes.length;
      for (var i:Number = 0; i<numPeople; i++) {
      aPerson[ i ] = new Object();
      aPerson[ i ].personName = xPersonData.firstChild.childNodes[ i ].childNodes[0].firstChild;
      aPerson[ i ].profession = xPersonData.firstChild.childNodes[ i ].childNodes[1].firstChild;
      aPerson[ i ].xmap = xPersonData.firstChild.childNodes[ i ].childNodes[2].firstChild;
      aPerson[ i ].ymap = xPersonData.firstChild.childNodes[ i ].childNodes[3].firstChild;
      }

      Later on depending on which button is pressed the following code is supposed to load some information on the selected person, as well as moving a circular pin symbol to a specific x/y coordinate on top of a map:

      LoadPic = function (imageNumber) {
      xco = aPerson[imageNumber].xmap;
      yco = aPerson[imageNumber].ymap;
      trace(xco);
      trace(yco);
      mc_largeImage.txt_name.text = aPerson[imageNumber].personName;
      mc_largeImage.txt_profession.htmlText = aPerson[imageNumber].from;
      mc_largeImage.mc_map.mc_pinmap._x = xco;
      mc_largeImage.mc_map.mc_pinmap._y = yco;
      }

      if I use the XML sample above, the trace of the two variables (xco and yco) reads out 150 and 25 but the symbol doesn't move anywhere. If I replace the xco behind mc_largeImage.mc_map.mc_pinmap._x = with a number it moves to a different spot. So it's not a matter of not talking to the symbol correctly...

      SO!! What's happening here? Can someone please help me?
      Is there something special about storing the number in an Array? Does it think it's a string and not a number?

      Would be greatful if someone could point me in the right direction :-)
      Thanks
      JJ
        • 1. Re: Not recognizing variable as number
          Greg Dove Level 4
          The contents of XML textNodes are of type String. And to access them you need to use the nodeValue property that is defined for textNodes. Otherwise, what you are accessing is actually an XMLNode, which traces out OK using its toString() method (which trace automatically uses), but doesn't provide you the string value if you use it in code. Actually using toString() method or nodeValue property should both work here.
          So the numbers you read into your xmap and ymap properties of your Person object are currently of type XMLNode.
          To get the string values and then convert them to numbers, try changing the following two lines:

          aPerson[ i ].xmap = xPersonData.firstChild.childNodes[ i ].childNodes[2].firstChild;
          aPerson[ i ].ymap = xPersonData.firstChild.childNodes[ i ].childNodes[3].firstChild;

          change to:

          aPerson[ i ].xmap =Number( xPersonData.firstChild.childNodes[ i ].childNodes[2].firstChild.nodeValue);
          aPerson[ i ].ymap = Number(xPersonData.firstChild.childNodes[ i ].childNodes[3].firstChild.nodeValue);
          • 2. Re: Not recognizing variable as number
            jjulie Level 1
            THANK YOU THANK YOU THANK YOU!!! :-)
            • 3. Re: Not recognizing variable as number
              Greg Dove Level 4
              No problems, you're welcome.