PDA

View Full Version : [AS3] Depth Control Assistance


EightySeven
03-04-2009, 02:15 AM
Hey ya'll this is what I got. It works fine for what I need it to do right now but I'd like it to improve on it. So I offer it to the community for suggestions

//...In the game loop
for (var i:int = 0; i<gang.length; i++) {
DepthControl(gang[i]);
}
//...
//The functions was built to move an object backwards in the display list depending on its y position.
//so if the Y value is lower it should be behind objects with a higher y position
private function DepthControl(mc:MovieClip) {
var mcDepth:int = gameLevel.getChildIndex(mc);
for (var i:int; i < gameLevel.numChildren; i++) {
var child = gameLevel.getChildAt(i);
if (child is Table || child is Bar ) {
if (child.y < mc.y) {
if (gameLevel.getChildIndex(mc) < i) {
gameLevel.setChildIndex(mc, i+1);
}
}
if (child.y > mc.y) {
if (gameLevel.getChildIndex(mc) > i) {
gameLevel.setChildIndex((mc), i);
}
}
}
}
}

The only major issue I have with it right now is that when I add || child is Mutant to the outer most if statement it get an index out of range error on the line

gameLevel.setChildIndex(mc, i+1);

Mutants are dynamically created and added to the level.

Any thoughts on improvement or how to correct my issues with the current function would be greatly appreciated

rrh
03-04-2009, 06:06 PM
What happens if the child is the last one? Does the error occur for all the mutants, or only the last one?

EightySeven
03-05-2009, 10:08 PM
Its always right after the game launches, so the first or second Mutant hard to tell since they spawn at the same time

rrh
03-06-2009, 04:54 AM
How you tell is you throw in some trace() statements, like trace(i) or something and see what's the last one printed before it breaks.

EightySeven
03-06-2009, 03:51 PM
Ok threw more testing I've gotten varying results which lead to even more filtering.

[object Mutant] is throwing the error and is the 9/40 Child. Is the 0 object in the gang array with a Ypos of:500

[object Mutant] is throwing the error and is the 39/40 Child. Is the 1 object in the gang array with a Ypos of:225
RangeError: Error #2006: The supplied index is out of bounds.
at flash.display::DisplayObjectContainer/setChildIndex()
at Here2Collect/newDepthControl()
at Here2Collect/gameLoop()

Thats the info. in this case it was the Second Mutant to throw the error..however I've recorded instances where it is the first Mutant to throw the error

EightySeven
03-10-2009, 02:52 AM
No ideas?

dungkos
04-07-2009, 07:37 AM
Flash will generate an error if you use
gameLevel.setChildIndex(mc, i+1);
with i = gameLevel.numChildren - 1.

Use swapChildren() in that case.

bluemagica
04-07-2009, 09:36 AM
can't you simply bubble sort the displaylist using swapChildren?? that would be better i think!