12-16-2009, 11:05 AM
|
#1
|
|
Registered User
Join Date: Dec 2009
Posts: 10
|
XML & array problem
Hello,
I'm trying to load XML data in an array (it is not finished yet because I need some help)... This will create levels for a breakout game, where 1 creates a block and 0 doesn't. I need to write an XML file storing the levels and the array content for the blocks. In the end I should be able to fill up an array like below using the extracted information!
ActionScript Code:
var lvl:Array = [[0,0,0,1,0,0,0],
[0,0,0,0,0,0,0]];
I have been trying so hard to get this done but can't find the correct solution so I was wondering if someone could give me a hint on how I should write the XML and actionscript to load the numbers into the array (note that I'll need an array in another array).
Thank you for your help!
|
|
|
12-16-2009, 11:20 AM
|
#2
|
|
tadster
Join Date: Feb 2009
Location: Texas
Posts: 2,106
|
so what you want to do is store an array in xml?
<myxml>
<thearray>[[0,0,0...],[..]]</thearray>
</myxml>
ActionScript Code:
//load the xml via a urlloader
//add a complete event listener
function completeHandler(e:Event):void
{
var thexml:XML = XML(e.target.data);
theArray:Array = Array(thexml.thearray);//try that
//you can also bring it in as a string then chop it up into an array
//based on "[", "]", and commas
}
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
Last edited by tadster; 12-16-2009 at 11:22 AM.
|
|
|
12-16-2009, 12:17 PM
|
#3
|
|
Registered User
Join Date: Dec 2009
Posts: 10
|
It works, thank you! I was using the same methods but much more complicated... The only thing with this method is that the end-user won't immediately know how to insert or remove blocks but no prob, I'm glad it works! Thx again for the quick reply!
|
|
|
12-16-2009, 01:55 PM
|
#4
|
|
Registered User
Join Date: Dec 2009
Posts: 10
|
Hmmm, when trying to fill the array with the xml for level 2 I get an error because I'm using the same var 2 times. When there are no blocks left on the stage to hit I just tell the currentLvl to add 1 using ++, this works. Then I call the function to create the level but now level 2 us ing the code below.
ActionScript Code:
if (currentLvl == 1) {
var lvlArray:Array = new Array (thexml.lvl1array);
}
if (currentLvl == 2) {
var lvlArray:Array = new Array (thexml.lvl2array);
}
What am I doing wrong?
Regards.
|
|
|
12-16-2009, 01:59 PM
|
#5
|
|
Holosuit User
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,299
|
Whatever you are trying to do (of which I'm not really sure...)
- don't declare variables inside loops or inside conditions.
- don't use Array's constructor - it is virtually unexisting function. Use Array literal - you won't get confused at least when trying to convert something to array or put an array inside another array.
EDIT: Ah, it seems like you ware trying to create an array from string - then look into Array#split([delimiter:String]); method e.g.:
ActionScript Code:
var array:Array = "a,b,c".split(",");
Last edited by wvxvw; 12-16-2009 at 02:02 PM.
|
|
|
12-16-2009, 02:04 PM
|
#6
|
|
tadster
Join Date: Feb 2009
Location: Texas
Posts: 2,106
|
i'm not sure what your trying to do, but :
ActionScript Code:
var lvlArray:Array;
if (currentLvl == 1) { lvlArray = new Array (thexml.lvl1array); }
if (currentLvl == 2) { lvlArray = new Array (thexml.lvl2array); }
is how that should be (don't mind the formating) "var lvlArray:Array" should only happen once, strictly speaking.
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
|
|
|
12-16-2009, 02:42 PM
|
#7
|
|
Registered User
Join Date: Oct 2009
Location: Washington, DC
Posts: 34
|
You could also just keep the XML elements the same (instead of naming them differently: lvl1array, lvl2array, etc)...
So your XML would be like this:
<myxml>
<thearray>[[0,0,0...],[..]]</thearray>
<thearray>[[0,1,0...],[..]]</thearray>
<thearray>[[1,0,1...],[..]]</thearray>
</myxml>
And you could just reference each "level" as a separate element in your XML Array... So level 1 would be lvlArray[0], level 2 would be lvlArray[1], etc...
|
|
|
12-20-2009, 09:58 AM
|
#8
|
|
Registered User
Join Date: Dec 2009
Posts: 10
|
I can't seem to be able to use this code in the code I already wrote... Is there anyone who would like to take a look at this code to help me? I'd really appreciate it and for someone who knows a lot about flash this should be easy...
Kind regards!
|
|
|
12-20-2009, 12:58 PM
|
#9
|
|
Holosuit User
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,299
|
ActionScript Code:
var xml:XML =
<a>
<a a="FE"/>
<a a="FD"/>
<a a="FB"/>
<a a="F7"/>
<a a="EF"/>
<a a="DF"/>
<a a="BF"/>
<a a="7F"/>
</a>;
function matrixFromXML(node:XML):Array/*Array*/
{
var arr:Array = [];
node.*.@a.(arr.push(arrayFromInt(parseInt(toXMLString(), 16))));
return arr;
}
function arrayFromInt(input:int):Array/*int*/
{
var i:int;
var k:uint = 1;
var arr:Array = [];
// 0xFF can hold up to 8 bits,
// You may make rows as long as 32 colums if you need.
// However, if you need more then 32 rows, then, you'll
// have to use different algorithm
while (k <= 0xFF)
{
arr[i] = (input >>> i) & 1;
k <<= 1;
i++;
}
return arr;
}
trace(matrixFromXML(xml).join("\r"));
/* Just an example:
0,1,1,1,1,1,1,1
1,0,1,1,1,1,1,1
1,1,0,1,1,1,1,1
1,1,1,0,1,1,1,1
1,1,1,1,0,1,1,1
1,1,1,1,1,0,1,1
1,1,1,1,1,1,0,1
1,1,1,1,1,1,1,0
*/
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 10:30 PM.
///
|
|