Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-30-2012, 02:59 PM   PM User | #1
Boldonglen
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Boldonglen is an unknown quantity at this point
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
Boldonglen is offline   Reply With Quote
Old 07-30-2012, 06:21 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
Boldonglen (07-31-2012)
Old 07-31-2012, 09:51 AM   PM User | #3
Boldonglen
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Boldonglen is an unknown quantity at this point
That works great thanks for your help DaveyErwin.
Boldonglen is offline   Reply With Quote
Reply

Bookmarks

Tags
asp, game, javascript, map, xml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:35 PM.


Advertisement
Log in to turn off these ads.