Groups | Blog | Home
all groups > flash actionscript > august 2007 >

flash actionscript : mental block with ARRAYS


nik c
8/14/2007 9:19:55 PM
Hi,

I got to loop through one array and selectively transfer values from it into
another array.

contentItems[ i ].attributes.id != "x" --- looks at an id attribute in an XML
sheet and if this is x the respective data is not transferred from one to the
other array.

As you can see the source data is from an XML file (.attributes.title, etc.).

i is the counter for the source array and k is the counter for the destination
array. I am using different ones, as I want the indexes of the destination
array to be continuous, whereas I found if I use i for this as well, I end up
with missing index numbers.

I am having a complete mental block at this moment in time - I am sure I am
not seeing the blinding obvious of why this does not work. Tracing 'contentArr'
in any shape or form after the transfer supposedly should take place (ie
further down the script) simply reports an 'undefined'


Thanks,

Nik C



var k:Number=0;

for (i=0; i<contentItems.length; i++) {

if (contentItems[i].attributes.id != "x") {

var vidArr:Array = new Array;
vidArr[i] = contentItems[i].childNodes;

for (j=0; j<vidArr[i].length; j++) {
contentArr[k][j][0] = vidArr[i][j].attributes.title;
contentArr[k][j][1] = vidArr[i][j].attributes.thumbURL;
contentArr[k][j][2] = vidArr[i][j].attributes.clipURL;
}

k++;

}

}
CaioToOn!
8/14/2007 9:48:09 PM
Hi, there.

I don't know if I understood well, but:

First of all, is the code below all the code you have? Because you need to
declare de array object, just like:

var k:Number = 0;
var vidArr:Array = new Array();
var contentArray:Array = new Array();


Second point, at the line "var vidArr:Array = new Array;" you forgot the
parentesis [new Array()]

And third one, if contentItems is a XML object, so you need to put
contentItems.childNodes.length instead of contentItem.length.

Did I got you doubt?

CaioToOn!
FlashTastic
8/14/2007 9:51:32 PM
nik c
8/14/2007 10:04:06 PM
Hi,

Thanks for the replies. Just realised the thing about the wrong place for the
vidArray definition. Belwo is the complete function and it still doesn't work
yet.

I am leaving the // trace(etc.) in there as you can see up top which point
wverything works (or at least traces). It all seems to fall apart at the ...

contentArr[k][j][0] = vidArr[i][j].attributes.title;

.... and i can't get my head 'round it.


Thanks,

Nik C




function loadContent(content_xml){
contentItems = content_xml.firstChild.childNodes; // point to start element
of XML
var i:Number=0 // number of UNIT notes
var k:Number=0 // independant counter for valid (non-"x") array elements
var vidArr:Array = new Array();
// trace(" ++++++ loadContent +++++++++++++++ contentItems.length: " +
contentItems.length); // WORKS

////////////////// SORT OUT THIS LOOP
for (i=0; i<contentItems.length; i++) {

// trace(" ++++++ loadContent +++++++++++++++ contentItems[i].attributes.id:
" + contentItems[i].attributes.id); // WORKS

if (contentItems[i].attributes.id != "x") {

// trace(" ++++++ loadContent +++++++++++++++
contentItems[i].attributes.id: " + contentItems[i].attributes.id); // WORKS

vidArr[i] = contentItems[i].childNodes;

trace(" ++++++ loadContent +++++++++++++++ vidArr[" + i + "]: " +
vidArr[i]); // WORKS
// trace(" ++++++ loadContent +++++++++++++++ vidArr[" + i + "]: " +
vidArr[i].length); // WORKS

for (j=0; j<vidArr[i].length; j++) {
trace(" ++++++ loadContent +++++++++++++++++++ vidArr[ " + i + " ][ " + j
+ " ]: " + vidArr[i][j].attributes.title);
contentArr[k][j][0] = vidArr[i][j].attributes.title;
trace(" ++++++ loadContent +++++++++++++++++++ vidArr[ " + i + " ][ " + j
+ " ]: " + vidArr[i][j].attributes.thumbURL);
contentArr[k][j][1] = vidArr[i][j].attributes.thumbURL;
trace(" ++++++ loadContent +++++++++++++++++++ vidArr[ " + i + " ][ " + j
+ " ]: " + vidArr[i][j].attributes.clipURL);
contentArr[k][j][2] = vidArr[i][j].attributes.clipURL;

trace(" ++++++ loadContent +++++++++++++++ contentArr[ " + k + " ][ " + j
+ " ][0]: " + contentArr[k][j][0]);

}
// trace(" ++++++ loadContent +++++++++++++++ contentArr[ " + k + " ]: " +
contentArr[k]);
k++;
}
}

// this traces out the elements of the contentArr
for (n=0; n<contentArr.length; n++) {
trace(" ++++++ loadContent +++++++++++++++ populated contentArr" + n + ": "
+ contentArr[n]);
}

this.populateSubs(0); // populate subs with unit 1 data

// set var to indicate ready for PLAY game action
_level1.allsReady = true;
}
GWD
8/14/2007 10:24:26 PM
I can't test it because I don't have the xml, but I would probably try to do it
like this.

function loadContent(content_xml) {
contentItems = content_xml.firstChild.childNodes;// point to start element of
XML
var i:Number = 0;// number of UNIT notes

var vidArr:Array;
for (var i = 0; i<contentItems.length; i++) {

if (contentItems[i].attributes.id != "x") {

vidArr = contentItems[i].childNodes;
var newContentIndex = contentArr.push([])-1;
for (j=0; j<vidArr.length; j++) {
contentArr[newContentIndex].push([vidArr[j].attributes.title,
vidArr[j].attributes.thumbURL, vidArr[j].attributes.clipURL]);
}
}
}

for (n=0; n<contentArr.length; n++) {
trace(" ++++++ loadContent +++++++++++++++ populated contentArr"+n+":
"+contentArr[n]);
}
this.populateSubs(0);// populate subs with unit 1 data

// set var to indicate ready for PLAY game action
_level1.allsReady = true;
}
nik c
8/14/2007 10:37:47 PM
YES!!!

That is what I have been trying to do!

And... I have just been working on a .push solution myself.

But I have never seen this syntax before:

[Q]var newContentIndex = contentArr.push([])-1;[/Q]

how does that work? You are setting the newContentIndex to the length of
contentArr -1 to get it to start from 0 by pushing an empty list onto the
array? is that right?

How dos it differ from e.g.
var k:Number=0;

and then
k++
inside the 'if'?

Thanks again,

Nik C
GWD
8/14/2007 10:51:13 PM
You're welcome.
The push method just adds a new element to the end of the array and returns
the new length of the array, so you don't need to use k when you use push.
What I did there wasn't very easy to read sorry, I just did it quickly. Both
of these are the same:

var newContentIndex = contentArr.push(new Array())-1;
var newContentIndex = contentArr.push([])-1;

This puts a new empty array as a new element at the end of your contentArr
array and gives us its index.

Then I pushed new elements into that new array which are themselves arrays of
the three attributes you're copying. It gets kinda complicated to describe.lol

If you do this type of thing again or often you may want to check out the
XPathAPI - I'm pretty sure you could use the xml itself rather than converting
it.

BTW I think you were missing out the new Array() or [] steps last time for
each new j copy copy you were making, which is why it wasn't working.
nik c
8/14/2007 11:01:46 PM
Cool!

It wasn't really a problem to read but I hadn't seen that syntax before and
just wanted to confirm that I understood it correctly! That XPathAPI looks like
a worthwhile thing for me to look into, as we will be doing a fair bit of XML
stuff in the near future!

The mental block is slowly dissolving, thanks for the aspirin!

Nik C
AddThis Social Bookmark Button