Cannot access a property or method of a null object reference.
at object/removeObject()
at document/objectCollide()
I’m trying to remove objects when they collide (these objects are all of the same MC and class and contained in the same array) At first the code works and objects are removed, but after the first two objects collide, I get the above error and from then onwards, the objects pass each other without accessing the collide function.
Currently I have a document.as file and an object.as file. My collision code in the document file is like so:
private function objectCollide(e:event):void{
for (var i:Number=0; i<object.length;i++){
for(var o:Number =0; o<object.length;o++){
if(i != o && objects*.hitTestObject(objects[o])){
objects*.removeObject();
}
}
}
}
and my removeObject function is located in the object.as and is like so:
public function removeObject(){
// I also remove the listeners attached to the object here
document(parent).removeChild(this);
}
I use the removeObject function to remove the objects when clicking on them also, and not had problems with it till now.
I have a feeling it could be a problem with the array (or the way I’m accessing it) I’ve been tracing out the array once an object has been removed from the stage. This has shown me that the array still contains the object - so I thought it could be getting confused when the object is in the array but not on the stage, so I had the objects removed from the array (once they had been removed from the stage). I then sorted the array(me thinking I would have a bunch of null array placeholders). And although it seemed to work for a bit longer, the problem still persisted.
my addObject code in the document.as is like so:
private function addObject(e:MouseEvent):void{
var clickObject:object = new object;
this.addChild(clickObject);
objectArray.push(clickObject);
}
Any ideas?