PDA

View Full Version : array containing movieclips doesn't behave as expected? (newbie)


onre
01-15-2006, 10:49 AM
Hello,

I'm quite new to Actionscript. What I've got here is a Flash application, which has a scrollpane containing multiple movieclip objects. The references to those movieclips are stored in an array called "items" which is global. Initially the scrollpane is empty, and the movieclips are created "on demand" as the user clicks on another sort of movieclips contained in another scrollpane. Here's the code responsible of creating the movieclips:


items[items.length]=attachMovie("ListItem", "item"+listitems.length,_parent.getNextHighestDepth()+li stitems.length,{_y:(listitems.length*20)});
items[items.length-1].index=items.length-1; // this is needed so that I can pass item index to the removal function, read further...
items[items.length-1].someProperty=someVariable; // some stuff passed as function arguments...


So, this works fine. I can add as many movieclips as I wish, and the arguments passed go to movieclip properties like they should. So, what's the problem, you ask? The problem comes along when I have to remove movieclips from the said scrollpane, or to be exact, when I add new movieclips after removing some. Here's the current version of code that does the removing ("index" is the index of movieclip that was clicked, gets passed to the function):


items[index].swapDepths(1);
items[index].removeMovieClip();
for(var i:Number=index; i<items.length; i++) {
items[i]=items[i+1];
items[i]._y=i*20;
items[i].index=i;
}
karaokelistitems.length--;


So here I remove the clip that was clicked on, recalculate the Y position for clips later in the array and shorten the array. This works fine, too. BUT. When I now add a new clip, odd things start to happen. When the creating function sets properties to the clip it created, those properties also get set for the clip whose index is same as the clip that I last removed! I did tracing after execution of each function, and here's what happens:


addItem called, args: fo
items[0] index=0 someProperty=foo _y=0
addItem called, args: bar
items[0] index=0 someProperty=foo _y=0
items[1] index=1 someProperty=bar _y=20
addItem called, args: quux
items[0] index=0 someProperty=foo _y=0
items[1] index=1 someProperty=bar _y=20
items[2] index=2 someProperty=quux _y=40
removeItem called, args: 1
items[0] index=0 someProperty=foo _y=0
items[1] index=1 someProperty=quux _y=20
addItem called, args:xyzzy
items[0] index=0 someProperty=foo _y=0
items[1] index=2 someProperty=xyzzy _y=40
items[2] index=2 someProperty=xyzzy _y=40


So, both '1' and '2' element of "items" array got modified by the creating function. I'm almost sure this has something to do with references? How should I modify the array in order to avoid this? I also tried using the Array.push and Array.splice to modify the array in a bit more "object-orientedish" way, but that didn't change the behaviour.

Hopefully this made any sense, I've been staring at the code for hours now so my output might not be the clearest possible :)