A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: [F8] XML Help

  1. #1
    Junior Member
    Join Date
    Jan 2007
    Posts
    2

    [F8] XML Help

    Hi,

    I am new to XML. Can anyone enlighten me on how to transfer layout of the map in Flash(refers to the codes below) to XML format?

    My codes in flash:
    Code:
    var array:Array = new Array();
    var mapArray:Array = new Array(
    1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,0,1,0,0,0,0,0,0,0,0,0,1,
    1,0,1,0,1,1,1,1,0,1,0,1,1,
    1,0,1,0,0,0,0,0,0,1,0,0,1,
    1,0,1,1,1,1,0,1,0,1,0,0,1,
    1,0,0,0,0,0,0,1,0,0,0,1,1,
    1,1,1,1,0,1,1,1,0,1,0,0,1,
    1,0,0,0,0,1,0,0,0,1,1,0,1,
    1,0,0,0,0,0,0,1,0,0,0,0,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1
    );
    
    //Numbers of row and columns in the Arrray
    var grass_mc:MovieClip = createEmptyMovieClip("grass_mc", 100);
    for(var row:Number = 0; row < 10; row++){
    	for(var col:Number = 0; col < 13; col++){
    		var i:Number = 13 * row + col;
    		grassArray[i] = grass_mc.attachMovie("grass" + mapArray[i], "grass" + i, i);
    		grassArray[i]._x = col*50;
    		grassArray[i]._y = row*50;
      }
    }
    Thanks

  2. #2
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    If your map consists only of "on/off" indicators, then there is little reason to use XML. If you intend to do more with it, let me know and I'll post an example. Thanks!

  3. #3
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    2 ways:
    1- manually trace out your xml file as text (loops just like your renderer through the arrays and returns the values in a way that it can be stored as XML
    2- work with the XML model within flash add childs and store attributes/ nadeValues to them. After you created the XML object simply trace it,- as it will show it just like you would see it in your browser, notepad,...

    the question is,- are you familar with xml,- if you want to save the file you also need be able to load it- and usually the loading part comes first. But maybe you want some xml- layout suggestion?

    in your case i´d write the XML something like:

    Code:
    <map name="mapTitle">
    	<n>1,1,1,1,1,1,1,1,1,1,1,1,1</n>
    	<n>1,0,1,0,0,0,0,0,0,0,0,0,1</n>
    	<n>1,0,1,0,1,1,1,1,0,1,0,1,1</n>
    	<n>1,0,1,0,0,0,0,0,0,1,0,0,1</n>
    	<n>1,0,1,1,1,1,0,1,0,1,0,0,1</n>
    	<n>1,0,0,0,0,0,0,1,0,0,0,1,1</n>
    	<n>1,1,1,1,0,1,1,1,0,1,0,0,1</n>
    	<n>1,0,0,0,0,1,0,0,0,1,1,0,1</n>
    	<n>1,0,0,0,0,0,0,1,0,0,0,0,1</n>
    	<n>1,1,1,1,1,1,1,1,1,1,1,1,1</n>
    </map>
    in flash then write something like this:
    Code:
    //loading xml
    xml_obj = new XML();
    xml_obj.onLoad = start;
    xml_obj.load("myXMLfile.xml");
    xml_obj.ignoreWhite = true;
    function start(success) {
    	if (success == true) {
    		init();
    	}else{
    		trace("error,- file not found?");
    	}
    }
    function init(){//inits once XML file is loaded
    	var array:Array = new Array();
    	var node = xml_obj.firstChild.childNodes;
    	for (var i=1;i<=node.length;i++){
    		array[i-1] = new Array();
    		var row = node[i-1].firstChild.nodeValue.split(",");
    		for (var j=1;j<=node.length;j++){
    			array[i-1][j-1] = int(row[j-1]);//convert text string to integer
    		}
    	}
    }
    //now build your map like you used to
    so did you knew this so far?

  4. #4
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Not to be rude, but there is zero reason to use XML if all you are going to do is dump it out the way renderhjs has listed. To steal the definition of XML:
    XML (Extensible Markup Language) is a W3C initiative that allows information and services to be encoded with meaningful structure and semantics that computers and humans can understand.
    Just using it to dump out rows of ones and zeroes defeats the purpose. Additionally, XML DOM processing (which is what Flash uses) is a very expensive operation (memory and CPU) just to read that in.

    If you simply want to externalize the string to make it easy to load in, just do something like this:

    10131111111111111101000000000110101111010111010000 00100110111101010011000000100011111101110100110000 1000110110000001000011111111111111

    The first two digits are the number of rows, the second two digits are the number of columns, then you simply need to do the loop like you did previously. This has the added advantage of letting you have maps up to 99 rows and 99 colums as opposed to your previously hard coded loops. Additionally, if you want to implement compression, you can do something like run-length encoding:
    http://en.wikipedia.org/wiki/Run-length_encoding

    There are a few implementations of that here on the forum that appear to work quite well.

    I'm a huge fan of XML, but I only suggest it's use where it makes sense. Have fun!

  5. #5
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    This thread hurts me. Physically.
    gamedozer games
    Free multiplayer and singleplayer games

  6. #6
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    @sand858:
    not like I care much about your pains but could you explain yourself?

  7. #7
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    It saddens me that my pain is not of your concern.

    <map name="mapTitle">
    <n>1,1,1,1,1,1,1,1,1,1,1,1,1</n>
    <n>1,0,1,0,0,0,0,0,0,0,0,0,1</n>
    <n>1,0,1,0,1,1,1,1,0,1,0,1,1</n>
    <n>1,0,1,0,0,0,0,0,0,1,0,0,1</n>
    <n>1,0,1,1,1,1,0,1,0,1,0,0,1</n>
    <n>1,0,0,0,0,0,0,1,0,0,0,1,1</n>
    <n>1,1,1,1,0,1,1,1,0,1,0,0,1</n>
    <n>1,0,0,0,0,1,0,0,0,1,1,0,1</n>
    <n>1,0,0,0,0,0,0,1,0,0,0,0,1</n>
    <n>1,1,1,1,1,1,1,1,1,1,1,1,1</n>
    </map>
    This part, in particular, hurts me.

    It is my hope that you proposed this as a solution only to severely simplify Mr. "xml"s need to modify his code to use XML. If you propose it as a reasonable XML conversion of his map data format, then, as I said, it causes me physical pain.

    Imagine that I am a superhero and that my kryptonite is evil obfuscating design choices and the misuse of technology. Then you'll understand why I cringe in my secret lair hoping that my sidekick, Spidergeek will destroy the impenetrable Dr. Hack's evil h4x0r device so that I can return to my daily life as a glorious superhero.
    gamedozer games
    Free multiplayer and singleplayer games

  8. #8
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    you dont seem to be helpfull here at all, start fighting somehwere else

  9. #9
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    Aw, you mistake my jesting as fightin' wurds!

    In all seriousness, webgeek made a great post. If you or "XML" is not to keen on XML, you should be sure to read it thoroughly.
    gamedozer games
    Free multiplayer and singleplayer games

  10. #10
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    your jesting insults me and your vague replies arent much constructive,

    - I am leaving this thread

  11. #11
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    Aw... sorry for rubbing you the wrong way. You're a better person for me for offering help to begin with. I'm sorry my jesting was insulting. My main goal was to provide some humor, my secondary goal was to point out that there might be more to XML than what you covered in your post. I apologize for offending you.
    gamedozer games
    Free multiplayer and singleplayer games

  12. #12
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    Since the indefatigable Sand858 and (vicariously) myself have been deemed unhelpful, I figured I'd go ahead and post something helpful to atone.

    I still feel your use of XML wasn't particularly elegant and as such I'm not going to bother posting an XML solution. I will simply expound on my original suggestion. The following code takes the same data as in the map array (assuming you've turned it into a string as I show below and suggested previously), run-length encodes the string, and then decodes it just to show how. Viola'

    Code:
    // Create our "cheater" way of defining a run length
    // This is better then using a straight number IF you know you wont exceed 52 in a single run. 
    // You can also use punctuation to get more.
    var letters:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var lookupByLetter:Array = new Array();
    var lookupByNumber:Array = new Array();
    for(var i:Number = 0; i < letters.length; i++) {
    	var letter:String = letters.charAt(i);
    	lookupByLetter[letter] = i;
    	lookupByNumber[i] = letter;
    }
    
    // Encode a string of numbers
    function encode(inputData:String):String {
    	var outputData:String = "";
    	for(var i:Number = 0; i < inputData.length; i++) {
    		var currentChar:String = inputData.charAt(i);
    		var hits:Number = 0;
    		for(var j:Number = i + 1; j < inputData.length; j++) {
    			var nextChar:String = inputData.charAt(j);
    			if(currentChar == nextChar) {
    				hits++;
    			} else {
    				break;
    			}
    		}
    		if(hits > 1) {
    			outputData += lookupByNumber[hits + 1] + currentChar;
    			i += hits;
    		} else {
    			outputData += currentChar;
    		}
    	}
    	return outputData;
    }
    
    // Decode a string of numbers
    function decode(inputData:String):String {
    	var outputData:String = "";
    	for(var i:Number = 0; i < inputData.length; i++) {
    		var currentChar:String = inputData.charAt(i);
    		var currentCode:Number = inputData.charCodeAt(i);
    		if(currentCode >= 48 && currentCode <= 57) {
    			outputData += currentChar;
    		} else {
    			var loopSize:Number = lookupByLetter[currentChar];
    			var nextChar:String = inputData.charAt(i + 1);
    			for(var j:Number = 0; j < loopSize; j++) {
    				outputData += nextChar;
    			}
    			i++;
    		}
    	}
    	return outputData;
    }
    Then to use it, you just do this:
    Code:
    var initialData:String = "1111111111111101000000000110101111010111010000001001101111010100110000001000111111011101001100001000110110000001000011111111111111";
    var encodedData:String = encode(initialData);
    var finalData:String = decode(encodedData);
    
    trace(initialData);
    trace(encodedData);
    trace(finalData);
    
    trace("Initial length: " + initialData.length);
    trace("Encoded length: " + encodedData.length);
    trace("Savings: " + ((encodedData.length / initialData.length) * 100) + "%");
    With the map posted initially, I get a savings of 54%, not too shabby.

    Now the code is ugly, uncommented, and brittle. It can only be used to encode strings of numbers due to it's use of letters to define run lengths. In short, it's crap but works fine as an example There are better run-legth encoding examples on this forum if you search. This was to show a better solution for saving/storing a simple map format.

    I love XML and use it for most everything but it is a sledgehammer when in this case you really needed a flyswatter.

    Have fun!

    EDIT: For some reason the forum adds wierd blank spaces in the long line of strings. Remove those to make things work properly. Odd...
    Last edited by webgeek; 01-09-2007 at 07:20 PM.

  13. #13
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    Boo! Killing a fly with a sledgehammer is far more fun than with a flyswatter!
    gamedozer games
    Free multiplayer and singleplayer games

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