Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-06-2012, 02:46 PM   #1
mrNoddy
Senior Member
 
Join Date: Aug 2009
Location: London, UK
Posts: 115
Default Tweening fails after array splice.

I have an array of display objects. I'm pushing new items into this array using the following function.

Code:
public function addSubItem(Item:DisplayObject, ID:int):void {
   trace('Adding item with ID: ', ID);
   if (!isAnyTweening(scrollerItemsArray)) {
    scrollerItemsArray.push(Item);
    scrollerIDArray.push(ID);
   
    Item.y = (scrollerSprite.y - Item.height);
    scrollerSprite.addChild(Item);
   
    for each (var i:DisplayObject in scrollerItemsArray) {
     TweenMax.to(i, 0.25, {y:i.y + (Item.height + 5)});
    }
   }
   
  }
And I'm removing the items from the array with this function:

Code:
public function removeSubItem(ID:int):void {
   trace('Remove item at position: ', ID);
   var removeItemPlace:int = scrollerIDArray.indexOf(ID, 0);  //get the location in the array of the item we are removing
   
   if (removeItemPlace == -1) {
    return;
   } else {
    var removeItemHeight:int = scrollerItemsArray[removeItemPlace].height;  //get the height of that item
    trace('remove height is: ', removeItemHeight);
    //remove the actual object
    scrollerSprite.removeChild(scrollerItemsArray[removeItemPlace]);
    //tween the items below that one upwards
    for (var i:int = 0; i < scrollerItemsArray.length; i++)
    {
     if (i == removeItemPlace) {
      break;
     }
     trace(i);
        TweenMax.to(scrollerItemsArray[i], 0.25, {y:scrollerItemsArray[i].y - (removeItemHeight + 5)});
    }
    //remove the item from the arrays
    scrollerItemsArray.splice(removeItemPlace);
    scrollerIDArray.splice(removeItemPlace);
   }
  }
Which is all working fine. However, once I've performed an item remove, if I then add another item, the tween will work on all but one item, the one 'above' the item which was removed. It's hard to describe, so here is a video:

http://www.youtube.com/watch?v=7NkBkoAejtI

Any ideas what I've done wrong here?

Thanks.
mrNoddy is offline   Reply With Quote
Old 08-06-2012, 05:06 PM   #2
thawfeek_yahya
Senior Member
 
thawfeek_yahya's Avatar
 
Join Date: Oct 2009
Location: India
Posts: 129
Send a message via Yahoo to thawfeek_yahya Send a message via Skype™ to thawfeek_yahya
Default

I believe you are dealing with one of the common programming errors. if you are going to remove an item from array use backward loop instead of forward loop.

use this

Quote:
var numCount:int = 30; //Number of items to iterate through the loop

for(var i:int=numCount;i>0;i--) {
//process your code here
}
thawfeek_yahya is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:07 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.