PDA

View Full Version : hitTest and attachMovie



leetleet
September 2nd, 2008, 04:13 PM
Ok so basically I got mobs spawning at intervals, using attachMovie(), name them mob1,mob2,mob3,etc. They run upward onEnterFrame towards some police dudes also spawned using attachMovie(), named police1,police2,etc.
Now when the mob hits the police, the mob should be removed from the stage.
On the mob_mc first frame I got


var count:Number = 1;

onEnterFrame = function(){
//moves mob upwards
_y -= Math.random()*10;

//if mob is offscreen, removemoveclip
if(_y <= -25){
removeMovieClip(this);
}

//if mob hits police, removemovieclip
for(count = 1; count <= 10; count++){
if(this.hitTest(_root["police"+count])){
removeMovieClip(this);
}
}
}
It works fine, until I happen to let one of the mobs by, then everything turns to crap. Sometimes more than one mob disappears, sometimes mobs go straight through the police(no hit detection). Yeh, I can't figure this one out.

bluemagica
September 2nd, 2008, 04:39 PM
try this._y instead of just _y

SparK_BR
September 2nd, 2008, 05:39 PM
use this.removeMovieClip(); or just removeMovieClip();

and Math.random(10);
and for(int count=1; count<=10; count++){ then you don't need the var count:Number = 1 declaration
the scope of the var would be only inside the for loop...

and make sure you have only 10 policemans all with the name police1 to police10
if you are attaching polices you should put a maxPolice var and try to use it in the for loop instead of 10


if this doesn't work, bash your head in the wall 3 times and scream "cucaracha" then throw your self through the window
(all your problems will be solved XD)

leetleet
September 2nd, 2008, 08:20 PM
Thanks Spark, but it only solved part of the problem. It seems this code only works for the 'newest' attached movie. If I let one go past and it hasn't reached the end of the stage, the hitTest for the next one doesn't register. Also if I hit a mob on the side as it is running up, all the mobs currently on the stage disappear.
I'll try your second reccomendation :)

bluemagica
September 2nd, 2008, 08:49 PM
leetleet do what i said, and if it doesn't work then upload your file!

_y is referencing the mob itself, but for that hittest to work right you need to refer to that particular instance, and hence you should use the keyword "this"

leetleet
September 2nd, 2008, 10:16 PM
I did try adding this._y, didn't change. I'll post up the fla.

SparK_BR
September 3rd, 2008, 10:15 AM
I did try adding this._y, didn't change. I'll post up the fla.

I think i had a mobCount++ somewhere...

I made a commentary about every single change, as always... :mu:

leetleet
September 3rd, 2008, 01:00 PM
Thanks again spark =p
That one line killed me >_>