A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Using XML Data for calculations

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    13

    Using XML Data for calculations

    Morning everyone,

    Im trying to do some calculations with data that has been pulled from an XML document. At the moment however the field is reporting back with NaN error, can anyone see where im going wrong? Heres my code atm

    Code:
    var totalItems:Number = 0;
    var myXML:XML = new XML();
    myXML.ignoreWhite = true;
    myXML.onLoad = function(success) {
    	if (!success) {
    		trace("XML FAILED TO LOAD");
    	} else {
    		//trace("XML LOADED");
    		totalItems = myXML.firstChild.childNodes.length;
    		//populate CB component
    		myBox_cb.addItem("Choose item");
    		for (i = 0; i < totalItems; i++) {
    			var infoName:String = myXML.firstChild.childNodes[i].attributes.infoName;
    			myBox_cb.addItem({data:i, label:infoName});
    		}
    	}
    };
    myXML.load("data/xml/breakfast.xml");
    // Add event listener and event handler function.
    var cbListener:Object = new Object();
    cbListener.change = function(evt_obj:Object):Void  {
    	var currentlySelected:Object = evt_obj.target.selectedItem;
    	if (currentlySelected.data != undefined) {
    		cal.text = myXML.firstChild.childNodes[currentlySelected.data].attributes.cal;
    	}
    };
    myBox_cb.addEventListener("change", cbListener);
    
    calTotal.text = 200 - cal.text;
    All help greatly appriceated.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    external data is received in Flash as a "string"
    you need to convert to "number"

    this line - calTotal.text = 200 - cal.text;
    will produce NaN as it compiles before the data has completed load/parse

    try -
    PHP Code:
    var cbListener:Object = new Object();
    cbListener.change = function(evt_obj:Object):Void  {
    var 
    currentlySelected:Object evt_obj.target.selectedItem;
    if (
    currentlySelected.data != undefined) {
    sel myXML.firstChild.childNodes[currentlySelected.data].attributes.cal;
    cal.text Number(sel);
    calTotal.text 200 Number(cal.text);    
    }
    };
    myBox_cb.addEventListener("change"cbListener); 

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    13

    resolved

    Thanks again a_modified_dog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center