|
-
How should I refer to these specific arrays?
Hello,
I'm making a shopping cart which has a checkout area where there are "slots" that are filled with a rectangle describing the items within it. Each slot is filled via an addChild using an array that ensures each slot is placed precicely below the previous slot. Here's the code for that (cartslot is the name of the movieclip):
Code:
function sendDVDToCheckout(evt:MouseEvent):void {
var slot:cartslot = new cartslot();
shoppingCart.addChild(slot);
var slotPos:Number = ((checkoutSlotsArray.length) * slot.height);
checkoutSlotsArray.push(slot);
itemCounter++;
slot.name = String(itemCounter);
slot.x = 0;
slot.y = slotPos;
slot.cartText.text = "Indoor Shred DVD";
for (var i:int = 0; i < checkoutSlotsArray.length; i++) {
checkoutSlotsArray[i].closeSlot.buttonMode = true;
checkoutSlotsArray[i].closeSlot.useHandCursor = true;
checkoutSlotsArray[i].closeSlot.addEventListener(MouseEvent.CLICK, clearSlot);
}
}
On each cartslot mc is a delete button that the user can click to remove that item from the checkout, hence the last few lines of code (the delete button has been named "closeSlot").
What I need to know is how to get each slot positioned below the deleted slot to move up and fill the gap that the deleted one caused. I know I need to tell it something like "for each movieclip that is higher in the array than the deleted one, move them up by mc.height and then delete the selected one from the array" but I'm not sure how to go about this.
It seems to me like this should be quite an easy task but I'm really not sure how to determine which one the user has clicked, and how to refer to all of the ones higher in the array than it.
I'm completely new to arrays so specific code help rather than cryptic clues is greatly appreciated 
Thanks.
-
Anyone want to give this a try? I'm sure any experienced array users could do this fairly easily
-
Anyone? Please?
-
Is this not as straightforward as I imagined?
-
Senior Member
Here is a simple example, which you have to apply to your situation:
PHP Code:
var myArray:Array = new Array(); for (var i:int = 0; i<6; i++) { var myMC:Mymc = new Mymc(); myMC.id = i; myMC.x = myMC.width * i; myArray.push ({clip:myMC,id:i}); myMC.addEventListener (MouseEvent.CLICK, cHandler); addChild (myMC); } function cHandler (event:MouseEvent):void { var _num:int = event.currentTarget.id as int; removeChild(MovieClip(event.currentTarget)); var spliced:Array = myArray.splice(_num, 1); var splicedArray:Array = new Array(); for (var i:int = 0; i<myArray.length; i++) { splicedArray.push({clip:myArray[i].clip,id:i}) } for (var j:int = 0; j<splicedArray.length; j++) { splicedArray[j].clip.x = splicedArray[j].clip.width * j; splicedArray[j].clip.removeEventListener (MouseEvent.CLICK, cHandler); splicedArray[j].clip.addEventListener (MouseEvent.CLICK, cHandler); } }
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks for your help cancerinform, however I've found a really simple way of doing it:
Code:
function clearSlot(evt:MouseEvent):void {
var spacer:int = 44.1;// set this to the height of each item
evt.target.parent.parent.removeChild(evt.target.parent);
for (var i:int = 0; i < checkoutSlotsArray.length; i++) {
if (checkoutSlotsArray[i].name == evt.target.parent.name) {
checkoutSlotsArray.splice(i, 1);
}
}
for (var j:int = 0; j < checkoutSlotsArray.length; j++) {
checkoutSlotsArray[j].y = j*spacer;
}
}
It was the "if (checkoutSlotsArray[i].name == evt.target.parent.name)" that I was really looking for. Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|