Hi there I am building a shoppin cart in flash and I am having a problem getting the purchase items list to work. Each item which gets added to the cart gets put into a movie clip and added into a purchased items moveClip which is on the stage. Each child item has a remove button attached to it. So if the user wants to remove the item they can. The problem is when I remove some of the items on the list I get a “Error: Parameter child must be non-null.” Can any one please help. I have attached my fla, It has lots of comments so you wil be able to see whats going on. Thanks David.
Here is the code aswell:
//The cart_array is where I want to store incoming items in.
var cart_array:Array = new Array();
//The items_array is where I want to store the "purchased_mc" MovieClips inside once they have been put on the stage.
var items_array:Array;
//Every time I click on the addtoCart_btn it pushes another item into the cart_array, and it also calls the cartCalc(); function.
addToCart_btn.addEventListener(MouseEvent.CLICK,on Click);
function onClick(evt:MouseEvent):void{
cart_array.push(1);
cartCalc();
}
//The cartCalc() function uses a for loop to generate a list of "Items"(which are MovieClips) onto the stage.
function cartCalc():void{
items_array = new Array();
//I use this while loop to clear the "Items" children from the parentclip. So it can recreate the list everytime I add an item.
while(shopCartDetails_mc.numChildren){
shopCartDetails_mc.removeChildAt(0);
}
for(var i:Number = 0; i < cart_array.length; i++){
var purchased_mc:Purchased = new Purchased();
//Each "Item" is placed directly under each other onto the stage;
purchased_mc.y = i*70;
//I name each purchased_mc added to the stage as a number.
purchased_mc.name = "" + i
purchased_mc.num_txt.text = "" + i;
//The "purchased_mc" clip has a remove_btn inside it. I attach a listener to each item.
purchased_mc.remove_btn.addEventListener(MouseEven t.CLICK,onRemove);
//I push the "purchased_mc" MovieClips into the items_array.
items_array.push(purchased_mc);
shopCartDetails_mc.addChild(purchased_mc);
}
}
function onRemove(evt:MouseEvent):void{
//I store the evt.target.parent.name into a variable called itemRemoved;
var itemRemoved:Number = evt.target.parent.name;
//The itemRemoved variable is used to locate the correct index of the items_array of the item we want to remove.
shopCartDetails_mc.removeChild(items_array[itemRemoved]);
//I use a for loop to look through the items_array and find out any of the items which were below the itemRemoved and then move them up by 70px.
for(var i:Number = 0; i < items_array.length; i++){
if(i >= itemRemoved){
items_array*.y -= 70;
}
}
//This removes the item from the items_array and the cart_array.
items_array.splice(itemRemoved,1);
cart_array.splice(itemRemoved,1);
trace("items_array.length is " + items_array.length)
trace("cart_array.length is " + cart_array.length)
}