Hi,
I’m building an app that requires the following…
-
User clicks on main navigation which leads to a function which reads an xml file and dynamically places links in a submenu based on the entries in the xml.
-
User clicks one of these links which then changes the text in a dynamic text field and an image in the man content section too.
-
When the user clicks another link on the main navigation menu, new links are dynamically inserted into the submenu and the old ones removed.
At the moment I have everything done except I cant remove the previous links when the new ones are placed.
I have it so that each link is added into a dynamic text field which is contained in a dynamic MovieClip for that link. Then the name of each movieclip (containing each separate link) is added to an array which was initiated at the start of the script.
Then, when a link is clicked from the main navigation, the names of the movieclips are read and a removeChild action is done for each within a for loop ranging from 0 to the array.length
However, every time the names of the movieclips are added into the array (with push()), they are inserted at the end of the array and then when the function to remove them is called, the movieclips already removed are still in the array and so a null object error is returned.
I have tried to set up a new array below the function to read the array and remove the movieclips but this again returns a null object error.
As i see it there are two options to solve this…
-
Find a way to create a new array after the function to read the array without returning the null object error.
-
Set up the for loop in the function to start at the position where the newest, relevant entries begin. However, this leads to the array becoming huge and I would like to avoid this.
I have it set the process up like so… (just an outline)
//Start the class
{
//create the array at the start
var mcArray:Array = new Array();
//function that operates when main navigation link is clicked
function mainNavClick(event:MouseEvent){
//loop to remove MovieClips
for (var i=0; i<mcArray.length; i++) {
var mcName = mcArray*;
flvLinkBar_mc.removeChild(mcName);
}
/*
All the xml is read and processed here
*/
for(var i = 0; i<flvLinkArray.length; i++){
var flvLinkHolder_mc:MovieClip = new MovieClip();
flvLinkHolder_mc.x = 5;
flvLinkHolder_mc.y = (i*42.3) + 10;
flvLinkHolder_mc.buttonMode = true;
//Create new TextField for each Link and assign its text
var flvLinkText:TextField = new TextField();
flvLinkText.htmlText = 'dataOut.product.flv[(number)].flvlink*.@title';
flvLinkHolder_mc.addChild(flvLinkText);
flvLinkBar_mc.addChild(flvLinkHolder_mc);
flvLinkHolder_mc.id = i;
mcArray.push(flvLinkHolder_mc);
//Add Mouse Click Listener to the movieclip
flvLinkHolder_mc.addEventListener(MouseEvent.CLICK, flvLinkHandler);
}
//Function to execute when each link is clicked
function flvLinkHandler (event:MouseEvent){
//Assign link number (from event variable) to new variable
var entryNum:String = event.currentTarget.id;
//Change the middle panel on the right to show the text associated with this link
secondaryTitleText_dt.text = dataOut.product.flv[(number)].flvlink[(entryNum)].@title;
secondaryDescText_dt.text = dataOut.product.flv[(number)].flvlink[(entryNum)].flvlinktext;
}
}
Currently the function will work when you click on a navigation link for the second time, on the third time it returns that the display object must be a child of the removeChild caller. I understand why this is hapenning but cant find a way around it.
If someone can tell me how to either wipe the array, create the array again after the function to remove the movieclips without getting an error or otherwise get the job done I’ll be very happy!
Many thanks!