PDA

View Full Version : Odd Array hitTest behavior.



SonOfChaos
July 2nd, 2009, 09:12 PM
Okay, been having some strange going on's here. When you click, this program creates an object on the stage which hitTest on another object already their. I have a fade effect on the 'blastArray' of objects. I figured that I could monitor them by their alpha property and when they have faded out I could simply remove them for nifty cleanup. Unfortunately the AS3 is not being friendly. The game will hitTest ONCE after being started and then never again with the following error.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Trip/Blast()

If I nest the alpha watching loop into the hitTest, I get this error.

TypeError: Error #1010: A term is undefined and has no properties.
at Trip/Blast()


I'm a noob so please explain in plain english.



public function Blast(evt:MouseEvent){

var ablast:acidBlast = new acidBlast();
blastArray.push(ablast);
addChild(ablast);
ablast.blastPath();

for(var b=0; b<blastArray.length;b++){
for(var s=0; s<boxArray.length;s++){
if(blastArray[b].hitTestObject(boxArray[s])){
//trace('HIT!!!!');
score += 5;
value1 += 1;
trace(blastArray);

boxArray[s].die();
blastArray[b].hit();
//removeChild(boxArray[s]);
boxArray.splice(s,1);
//removeChild(blastArray[b]);
blastArray.splice(b,1);

}
if(blastArray[b].alpha <= .30){
removeChild(blastArray[b]);
}
}
}

Shaedo
July 3rd, 2009, 12:05 AM
not sure what is wrong BUT, until someone more talented solves your problem, this might help
try changing this
removeChild(blastArray[b]);

to this
blastArray[b].parent.removeChild(blastArray[b]);

Shaedo
July 3rd, 2009, 12:10 AM
Ahh ok I think maybe the other problem is this.

you run this code
removeChild(blastArray[b])
but this will not change the length of your blastArray
so it still runs through all the blastArrays and when it gets to the ones that have an alpha that is less than 30 it trys to remove them. Which you already did from the stage but not the array. If I am correct then the length of the blastArray is constant.

One solution is to simply remove them from the array. Another is to have them only removed when their alpha == a set number rather than <=. Another solution is to check if stage == true and only remove them if it is.

does this make sense ?

SonOfChaos
July 3rd, 2009, 01:07 AM
Yes, it makes sense, I implemented your suggestion and it worked. Thanks, I have found other issues as well in the process.

I moved the splice function INTO the 'if alpha' check. This way nothing is deleted until it is a certain amount off the stage and then the removeChild runs and the Array entry is 'spliced'.

Now I have another problem, when I click and hit something, and then click on something else, it keeps adding to the score even if I am not hitting something, it seems to be registering the same hit over and over and I'm not certain how to fix this.

Updated code below.



for(var b=0; b<blastArray.length;b++){
for(var s=0; s<boxArray.length;s++){
if(blastArray[b].hitTestObject(boxArray[s])){
//trace('HIT!!!!');
score += 5;
value1 += 1;
trace(blastArray);
boxArray[s].die();
blastArray[b].hit();

if(boxArray[s].alpha <= .02){
removeChild(boxArray[s])
boxArray.splice(s,1);

}
if(blastArray[b].alpha <= .02){
blastArray.splice(b,1);
removeChild(blastArray[b]);

trace('remove entry');
}

}

}
}

SonOfChaos
July 3rd, 2009, 01:41 AM
It appears that the removeChild is not working cause it is not the 'child of a caller'. anyone know how to fix this/access/find the parent?