Ok this has been driving me nuts for the past couple of hours and i know there is an easy answer.
I have a scrollPane which has a movie clip called right_container_mc as it's source.
Inside this right_container_mc I have other other movie clips called execiseBox that get added (in the correct positions on the stage) from an array with a for loop.
Each exercise box has a button symbol called close_btn.
Firstly I'm not sure that this is the best way to achieve this so feel free to suggest a better way!
What I want to do is when this close_btn is clicked remove the specific exerciseBox movieclip from the array and from the stage then loop through the array again so all of the exercise box movieclips update their position on the stage.
I am having trouble getting a reference to the movie clip because it is nested to remove it from the array and stage. Here is the code I have so far, need to add in the removing and updating parts. Also is should I be removing all of the current instances of the exerciseBox movie clips before the array loop runs each time??
Any help is greatly appreciated.
function addMovieClipsToStage(event:MouseEvent):void
{
scrollPaneRight.source = right_container_mc;
exerciseBox = new Exercisebox();
exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
boxArray.push(exerciseBox);
sortBoxes();
scrollPaneRight.update();
}
function onRemoveBox(event:MouseEvent):void
{
}
function sortBoxes():void
{
for (var i:int =0; i<boxArray.length; i++)
{
right_container_mc.addChild(exerciseBox);
exerciseBox.x = 0;
exerciseBox.y = ((115 + 3)*i);
}
}
in your onRemoveBox function
event.currentTarget should return the object that function was triggered by.
Seeing as that object is a child of right_container_mc maybe you could try:
right_container_mc.removeChild(event.currentTarget);
Based upon the code you have posted I'm not sure you even need an array. It looks like you're using it to keep track of the number of children. Display list already does this for you.
So I think your sort could just reference right_container_mc.numChildren
instead of an array length.
Hope some of this helps!
In order to get rid of the boxArray, you could just loop through all the ExerciseBoxes in right_container_mc.
function addMovieClipsToStage(event:MouseEvent):void
{
scrollPaneRight.source = right_container_mc;
exerciseBox = new Exercisebox();
exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
right_container_mc.addChild(exerciseBox);
sortBoxes();
scrollPaneRight.update();
}
function onRemoveBox(event:MouseEvent):void
{
right_container_mc.removeChild(event.currentTarget);
sortBoxes();
}
function sortBoxes():void
{
var count:int = 0;
for each(var exerciseBox:Exercisebox in right_container_mc)
{
count++;
exerciseBox.x = 0;
exerciseBox.y = (115 + 3) * count;
}
}
For more information on 'for each ... in', check out http://help.adobe.com/en_US/AS3LCR/Flash_10.0/statements.html#for_each..in
For some reason the for each loop in the sortBoxes function is not firing. I have added a trace statement to check this and nothing happens, here is the updated code:
public function addMovieClipsToStage(event:MouseEvent):void
{
scrollPaneRight.source = right_container_mc;
exerciseBox = new Exercisebox();
exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox);
exerciseBox.x = 0;
exerciseBox.y = (118 * exerciseBoxAmt);
right_container_mc.addChild(exerciseBox);
exerciseBoxAmt++; // the position of the boxes added to stage variable
sortBoxes();
scrollPaneRight.update();
}
public function sortBoxes():void
{
var count:int = 0;
for each (var exerciseBox:Exercisebox in right_container_mc)
{
exerciseBox.x = 0;
exerciseBox.y = (118 * count);
count++;
trace(count); //does not display in output window!!
}
}
public function onRemoveBox(event:MouseEvent):void
{
right_container_mc.removeChild(event.currentTarget.parent);
exerciseBoxAmt--;
sortBoxes();
}