Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Loading XML into Multidimensional array

    I have been creating a game in HTML5 and javascript and have came across a problem.

    The game uses a tile system to load the map. Currently my map is saved within a multidimensional array and looks like this:

    Code:
        var map = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                    [1, 3, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    I would like to add move levels by using a XML file to update the array.

    My XML file currently looks like this:

    Code:
        <TileMaps>
        <Level level="1">
        <map>[ 	[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    		[1, 3, 0, 0, 0, 0, 2, 4, 0, 1],
    		[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
        </map>
        </Level>
        <Level level="2">
        <map>[ 	[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    		[1, 3, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
    		[1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
    		[1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
    		[1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
    		[1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
    		[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    		[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
        </map>
        </Level>
        </TileMaps>
    If anyone could help me load level="1" into my map variable that would be great.

    Thanks

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    if your files are on
    a server , like this ...

    Code:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
    </head>
    <body>
    <script>
    var map = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                    [1, 3, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 2, 0, 4, 1],
                    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
    alert(map);
    req=new XMLHttpRequest();
    req.open("GET","my.xml",false);
    req.send();
    xmlDoc = req.responseXML;
    //eval("map="+xmlDoc.getElementsByTagName('map')[0].firstChild.nodeValue);
    map = JSON.parse(xmlDoc.getElementsByTagName('map')[0].firstChild.nodeValue);
    alert(map)
    </script>
    	</BODY>
    </HTML>
    Last edited by DaveyErwin; 07-30-2012 at 06:36 PM.

  • Users who have thanked DaveyErwin for this post:

    Boldonglen (07-31-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That works great thanks for your help DaveyErwin.


  •  

    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
    •