Basically, I have two routines: One is a CDK collision check, and the other is a generic verification of an array. They're both inside the same Timer Event. There are two arrays - the collisionList and the MasterArray, and the object is in both of them.
First, the collision routine:
var collisions:Array = collisionList.chec开发者_开发技巧kCollisions();
for(var i:uint = 0; i < collisions.length; i++)
{ var firstShape:Sprite = collisions[i].object1;
if(firstShape.name=="Obj1") {
collisions[i].object1.x = -20; collisionList.removeItem(collisions[i].object1); } }Then I have:
for each(var i in MasterArray) {
Shape1:Sprite = MasterArray[i];
if (i.x < 0) { removeChild(Shape1); MasterArray.splice(this,1); }
}
But it doesn't work. It gives me a massive crash. If I don't change the object x in the collision routine, the moment it's moved out of the screen by any other function, it disappears and all is well.
However, even if I just touch on it with the collision routine (for example, if I state I want its x at 20), the next time something happens and moves it to x < 0, I get the same crash.
If I don't do anything on the MasterArray check and do a removeChild on the collision check, it works fine too.
This is the error I get in either case:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild() at game2_Scene1_fla::MainTimeline/TimeCheck() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()
Thanks!
collisionList.removeChild(Shape1)
is a problem imho: is Shape1:Sprite = MasterArray[i];
really a child of collisionList
? and what type is collisionList
?
I think you are using the for each loop wrong in your second chunk of code.
Each i in MasterArray is a Sprite, no?
You're checking the x position of i, which seems right, but setting Shape1 to MasterArray[i] is probably setting Shape1 to null. (You're looking for an item in the MasterArray with the key 'i', which is actually an item from the MasterArray...)
So then you're trying to removeChild(null) and everything is blowing up.
You probably want to change your loop to a regular for loop. Something like this:
for(var i:int=MasterArray.length-1; i>-1; i--) {
shape1:Sprite = MasterArray[i];
if (shape1.x < 0) {
removeChild(shape1);
MasterArray.splice(i,1);
}
}
精彩评论