views:

40

answers:

2

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.checkCollisions();

 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!

A: 

collisionList.removeChild(Shape1) is a problem imho: is Shape1:Sprite = MasterArray[i]; really a child of collisionList? and what type is collisionList?

www0z0k
Actually, that removeChild(Shape1) is just that, ignore the collisionList before - that's garbage from when I was cleaning up. It doesn't work regardless though.
Carlo
@Carlo: so how does your problem code look like now?
www0z0k
A: 

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); 
     }
}
Cadin
That works whenever I don't initialize the collision routine. For example, if I set if (shape1.x < 400) { removeChild(shape1); MasterArray.splice(i,1); }Everything right at the start will be removed. That's right. However, when I collide with any of the objects (and they're removed by collision routine), the next moment there's a shape1.x < 400, I get the same error.I'm thinking that I need to remove the object from the MasterArray during the collision routine too, but how can I do that exactly, if they're in two different arrays?
Carlo
Hmm, well it's not exactly clear what's going on in the top block of code. What is collisionList? What does collisionList.removeItem() do? Is it possible that the item is already getting removed from the stage there? In any case, you can remove the item from MasterArray in that loop by first finding the index: MasterArray.indexOf(collisions[i].object1). That will return the index of the item in MasterArray. You can then use that index to splice it out of the MasterArray.
Cadin