# How to alter array without causing problems

**URL:** <https://forum.kirupa.com/t/how-to-alter-array-without-causing-problems/327097>\
**Category:** flash\
**Created:** [February 6, 2012, 6:18am UTC](https://forum.kirupa.com/t/how-to-alter-array-without-causing-problems/327097 "2012-02-06T06:18:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kappa](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Kappa](https://forum.kirupa.com/u/Kappa)\
**Post date:** [February 6, 2012, 6:18am UTC](https://forum.kirupa.com/t/how-to-alter-array-without-causing-problems/327097/1 "2012-02-06T06:18:35Z")

</div>

I’m trying to work on a presentation/slideshow, where I will have to insert and remove pages quite often. I had the idea that I could just change the array by setting it from each movie clip (each movieclip is a slide or page).

this is the script on my root

```auto
//array setup
var pageArray:Array = [new page1(), new page2(), new page3(), new page4(), new page5];**
var currentPage:Number = 0;

addChild(pageArray[currentPage]);

//go to next page function
function nextPage():void
{
	if (currentPage < pageArray.length - 1)
	{
		TweenMax.to(pageArray[currentPage], 1, {cacheAsBitmap:true, x:-1024, onComplete:removeChild, onCompleteParams:[pageArray[currentPage]]});
		currentPage++;
		pageArray[currentPage].x = 1024;
		addChild(pageArray[currentPage]);
		TweenMax.to(pageArray[currentPage], 1, {cacheAsBitmap:true, x:0, onComplete:pageArray[currentPage].startAnimation});
	}

}

//go to prev page function
function prevPage():void
{
	if (currentPage > 0)
	{
		TweenMax.to(pageArray[currentPage], 1, {cacheAsBitmap:true, x:1024, onComplete:removeChild, onCompleteParams:[pageArray[currentPage]]});
		currentPage--;
		pageArray[currentPage].x = -1024;
		addChild(pageArray[currentPage]);
		TweenMax.to(pageArray[currentPage], 1, {cacheAsBitmap:true, x:0, onComplete:pageArray[currentPage].startAnimation});
	}
}

```

Then on for example the movieclip page3, I have this:

```auto
import com.greensock.*;
import flash.events.MouseEvent;

//startAnimation is called when the tween finishes on the root via onComplete
function startAnimation():void{
MovieClip(root).pageArray = [new page1(), new page2(), new page3(), **new extra1(), new extra2(),** new page4(), new page5];
//i have tried to insert 2 new pages "extra1" and "extra2"
}

/*
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

function onRemovedFromStage(event:Event):void{
trace("removed");
}
*/

```

I hoped it would just take the new pageArray I’ve set and continue on like usual but it seems to be causing a few issues. pageArray[currentPage] is no longer recognised for that slide and I also get a _“ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.”_ error.

I understand there is a correct way to do this with array splicing to insert the new items but I have multiple options that need to be added/removed so adding or removing based on index will be difficult later.
