A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: AS2 - Comparing values from XML to Variables in flash?

  1. #1
    Member
    Join Date
    Feb 2002
    Location
    Swansea (UK)
    Posts
    73

    Question AS2 - Comparing values from XML to Variables in flash?

    Hey Guys,

    Im stuck here (first time using xml with flash & first proper look at actionscript). I've got an xml file that contains some data (which will be created by an external file later on in the project) and i want to be able to check the data in that XML file against a variable stored in flash.

    If the value in the XML is equal to or greater than the value in the variable i want to jump to a different frame. If the xml value is less that the variable value i want it to stop and tell the user a message. i've included my code below. I just cant get it to work. Regardless of if the value in the XML file is higer or lower than the variable in flash, it always continues to the final frame instead of stopping accordingly. I'mm guessing its something easy...i hope!

    Cheers

    ActionScript
    Code:
    //Load xml file that contains amount of cash user has and populate cash_txt box with amount
    function loadXML(loaded) {
    	if (loaded) {
    		//.amount is the amount of cash user has in their account
    		_root.amount = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
    		
    		//.needed is the amount of cash needed to play
    		_root.needed = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
    
    		// test the amount of cash in users account, by displaying in cash_txt.text	
    		cash_txt.text = _root.amount;
    		
    		//store the users cash value in variable 'cash'
    		var cash  = _root.amount;
    		
    		//store the level of cash needed to play in variable 'desired'		
    		var desired  = _root.needed;
    
    	// If the XML file is not loaded stop at the current frame and display NO XML as trace
    	} else {
    		stop();
    		trace("NO XML");
    	}
    }
    //load in the XML and remove whitespace
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("ccCheck.xml");
    
    //If value of cash IS greater than or equal to value of desired goto and play 
    //frame 10 and display YOU CAN PLAY as trace 
    if (cash >= desired) {
    	gotoAndPlay(10);
    	trace("You can play");
    	
    //If value of cash IS NOT greater than or equal to value of desired stop on 
    //current frame and display error to user in error_txt field
    } else {
    	stop();
    	trace("user hasnt got enough cash to play");
    	error_txt.text = ("You dont Have Enough Cash");
    }
    XML
    Code:
    <?xml version="1.0"?>
    
    <user>
    	<cash>
    		<amount>1999</amount>
    		<needed>20000</needed>
    	</cash>
    </user>

  2. #2
    Member
    Join Date
    Feb 2002
    Location
    Swansea (UK)
    Posts
    73

    resolved Fixed

    Hey guys,
    Fixed it by creating a function and calling the function into the onLoad so that the variables from the XML are loaded before the function runs.

    Daz

Tags for this Thread

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