Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
mrEmpty

Tweening fails after array splice.

Recommended Posts

Hello.

 

OK, knowing Greensock I 100% understand that this problem is with my code.

 

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

 

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:

 

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://youtu.be/7NkBkoAejtI

 

Any ideas what I've done wrong here? I know this isn't Greensock specific, but this is the best forum for finding people who really know Flash.

 

Thanks.

Link to comment
Share on other sites

thanks for the video and for acknowledging that this is outside of the normal bounds of GreenSock support.

 

It is a bit difficult to assess what is happening from the video as it all happens VERY quickly.

 

I think this relates to how you are using splice.

If looping through the array doesn't tween all the items in the array, I would FIRST verify that the array contains everything that I think it should.

 

---

 

When using splice to remove a single element you need to specify the insertion point of the removal AND the amount of items to be removed.

 

consider the following:

 

BAD

var master:Array = ['apple', 'banana', 'cabbage', 'donut', 'eel', 'fennel', 'gnochi', 'ham'];
trace(master);
master.splice(2)// removes everything from cabbage to ham
trace(master); // apple, banana

 

GOOD : remove only cabbage:

var master:Array = ['apple', 'banana', 'cabbage', 'donut', 'eel', 'fennel', 'gnochi', 'ham'];
trace(master);
master.splice(2, 1)
trace(master); // apple,banana,donut,eel,fennel,gnochi,ham

 

 

try doing:

 

scrollerItemsArray.splice(removeItemPlace, 1);
scrollerIDArray.splice(removeItemPlace, 1);

Link to comment
Share on other sites

cool!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×