I’ve been adding and removing objects from my stage. Just when I think one part is fixed, another breaks.
Basically, I’m getting the following error -
RangeError: Error #2006: The supplied index is out of bounds.
at flash.display::DisplayObjectContainer/setChildIndex()
at flash.display::Stage/setChildIndex()
at tme3_Scene2_fla::MainTimeline/fireBulletHandler()
at runtime. The code looks like this -
function fireBulletHandler(evt:Event):void {
//create a temporary variable to store the bullet and set it's properties
//may need to push the bullet object into another array to track object position
var tempBullet:MovieClip = bulletArray.pop();
//add a new bullet to the numBulletOnStage variable - may use later?
numBulletOnStage = numBulletOnStage + 1;
//update the bullet value field - need to rethink
if (numBulletOnStage < 30) {
bulletText.text = "Bullets Left: " + (30 - numBulletOnStage);
}
//modified for TME 3 - go to last scene and clean up garbage objects
else {
bulletText.text = "Bullets Left: 0";
zombieTimer.stop();
//this loop cycles through all the objects on the stage and removes them except fot the
//main time line (the last object).
while(stage.numChildren > 1) {
//remove Event Listeners
stage.getChildAt(0).removeEventListener(Event.ENTER_FRAME, moveBullet);
stage.getChildAt(0).removeEventListener(Event.ENTER_FRAME, bulletHit);
stage.getChildAt(0).removeEventListener(TimerEvent.TIMER, makeZombie);
stage.getChildAt(0).removeEventListener(MouseEvent.CLICK, fireBulletHandler);
stage.getChildAt(0).removeEventListener(Event.ENTER_FRAME, moveZombieYOne);
stage.getChildAt(0).removeEventListener(Event.ENTER_FRAME, moveZombieYTwo);
stage.getChildAt(0).removeEventListener(Event.ENTER_FRAME, moveZombieYThree);
//remove the object from the stage
stage.removeChildAt(0);
}
Mouse.show();
stop();
//goto the next scene
gotoAndStop(1,"Scene 3"); // new line - tme 3
}
//set the scale of the bullet
if (tempBullet.width>tempBullet.height) {
tempBullet.width = 100;
tempBullet.scaleY = tempBullet.scaleX;
}
else if (tempBullet.height>tempBullet.width) {
tempBullet.height = 80;
tempBullet.scaleX = tempBullet.scaleY;
}
//set the properties of the bullet - position and direction
tempBullet.x = mouseX - 15;
tempBullet.y = mouseY + 25;
//this code causes the muzzle flash of the gun
newGun.gotoAndPlay(5);
//this calls the moveBullet function once the frame is entered.
tempBullet.addEventListener(Event.ENTER_FRAME, moveBullet);
//add the bullet to the stage
//add the bullet on top of the zombies (midway between the zombies and the gun)
// stage.addChildAt(tempBullet, numZombieOnStage + numBulletOnStage);
stage.addChild(tempBullet);
stage.setChildIndex(tempBullet, numZombieOnStage + numBulletOnStage);
//need to push the bullet into another array here for the hitTestObject****
stageBulletArray.push(tempBullet);
//set the gun closest to the viewer (top level of stage array) and reset the index of the stage elements
stage.setChildIndex(newBackground, 0);
stage.setChildIndex(newScoreCard, 1);
stage.setChildIndex(scoreText, 2);
stage.setChildIndex(bulletText, 3);
stage.setChildIndex(newGun, stage.numChildren - 2);
tempBullet.addEventListener(Event.ENTER_FRAME, bulletHit);
} //~end fireBulletHandler
The problem I’m sure is here -
// stage.addChildAt(tempBullet, numZombieOnStage + numBulletOnStage);
stage.addChild(tempBullet);
stage.setChildIndex(tempBullet, numZombieOnStage + numBulletOnStage);
So I have the following questions -
This is a first person shooter and I’m adding bullets and zombies (as well as removing them) at different times. The objects are in 3d space and I emulate this by scaling and setting the z position of each object.
It worked fine before I started to clean up the objects from the stage using the removeChild function.
I’m guessing that there are holes in the display somehow and this is where as3 is choking, so -
-
Do I have to do a z-sort of the stage objects before I add a new object to the stage, everytime?
-
If yes to the above, will this fix my problem?
-
Are there examples of z-sorts that i can use? I’m not just sorting one type of object, I’m sorting multiple objects in relation to one another in 3D space - ie. gun, background, bullets, zombies, scoreboard etc.
If I’m not on the right track, could you give me any ideas? I’ve been stuck on this for months. Really struggling with garbage collection in as3.
Thanks.