Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-16-2009, 11:05 AM   #1
Registered User
 
Join Date: Dec 2009
Posts: 10
Default 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!
waftock@gmail.com is offline   Reply With Quote
Old 12-16-2009, 11:20 AM   #2
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

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.
tadster is offline   Reply With Quote
Old 12-16-2009, 12:17 PM   #3
Registered User
 
Join Date: Dec 2009
Posts: 10
Default

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!
waftock@gmail.com is offline   Reply With Quote
Old 12-16-2009, 01:55 PM   #4
Registered User
 
Join Date: Dec 2009
Posts: 10
Default

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.
waftock@gmail.com is offline   Reply With Quote
Old 12-16-2009, 01:59 PM   #5
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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(",");
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it

Last edited by wvxvw; 12-16-2009 at 02:02 PM.
wvxvw is offline   Reply With Quote
Old 12-16-2009, 02:04 PM   #6
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

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...
tadster is offline   Reply With Quote
Old 12-16-2009, 02:42 PM   #7
midnit
Registered User
 
Join Date: Oct 2009
Location: Washington, DC
Posts: 34
Send a message via AIM to midnit
Default

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...
midnit is offline   Reply With Quote
Old 12-20-2009, 09:58 AM   #8
Registered User
 
Join Date: Dec 2009
Posts: 10
Default

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!
waftock@gmail.com is offline   Reply With Quote
Old 12-20-2009, 12:58 PM   #9
wvxvw
Holosuit User
 
wvxvw's Avatar
 
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw
Default

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 */
__________________
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Reply


Thread Tools
Display Modes 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. The time now is 04:21 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.