A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How should I refer to these specific arrays?

  1. #1
    Senior Member
    Join Date
    Feb 2003
    Posts
    129

    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.

  2. #2
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    Anyone want to give this a try? I'm sure any experienced array users could do this fairly easily

  3. #3
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    Anyone? Please?

  4. #4
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    Is this not as straightforward as I imagined?

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Here is a simple example, which you have to apply to your situation:
    PHP Code:
    var myArray:Array = new Array();
    for (var 
    i:int 0i<6i++)
    {
        var 
    myMC:Mymc = new Mymc();
        
    myMC.id i;
        
    myMC.myMC.width i;
        
    myArray.push ({clip:myMC,id:i});
        
    myMC.addEventListener (MouseEvent.CLICKcHandler);
        
    addChild (myMC);
    }
    function 
    cHandler (event:MouseEvent):void
    {
        var 
    _num:int event.currentTarget.id as int;
        
    removeChild(MovieClip(event.currentTarget));
        var 
    spliced:Array = myArray.splice(_num1);
        var 
    splicedArray:Array = new Array();
        for (var 
    i:int 0i<myArray.lengthi++)
        {
            
    splicedArray.push({clip:myArray[i].clip,id:i})
        }
        for (var 
    j:int 0j<splicedArray.lengthj++)
        {
            
    splicedArray[j].clip.splicedArray[j].clip.width j;
            
    splicedArray[j].clip.removeEventListener (MouseEvent.CLICKcHandler);
            
    splicedArray[j].clip.addEventListener (MouseEvent.CLICKcHandler);
        }

    - The right of the People to create Flash movies shall not be infringed. -

  6. #6
    Senior Member
    Join Date
    Feb 2003
    Posts
    129
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center