PDA

View Full Version : Beginner hittest problem


max1x106
02-24-2007, 03:49 PM
I have 10 instances of a mc (Ball_mc1, Ball_mc2, ...) created at runtime. I have attached a reference to each instance in a unique array element: ball_array[1, 2, ...]. In a loop I have code to make the balls wander around triggered by an onEnterFrame event. I also have code which I hoped would detect collisions between the balls but it does not. Below is the code I have and line eight is where I think I am doing something wrong.

1 ball_array = new Array(11)
2 this.createEmptyMovieClip("holderClip", this.getNextHighestDepth());
3 for (var i:Number = 1; i <= 10; i++){
4 ball_array[i]=holderClip.attachMovie("Ball_mc", "Ball_mc" + i, holderClip.getNextHighestDepth(),
{_x:random(550), _y:random(400)});
5 ball_array[i].onEnterFrame=function(){
6 Wander around code
7 for (j = 1; j <= 10; j++){

8 if (ball_array[i].hitTest(ball_array[j])){setProperty (ball_array[j], _alpha, 20);}

9 }//eo sec loop over j
10 }//eo enter frame event
11 }//eo primary loop over i

max1x106
02-24-2007, 11:07 PM
I have stumbled onto the following working version:

ball_array = new Array(11)
this.createEmptyMovieClip("holderClip", this.getNextHighestDepth());
for (var i:Number = 1; i <= 10; i++){
ball_array[i]=holderClip.attachMovie("Ball_mc", "Ball_mc" + i, holderClip.getNextHighestDepth(),
{_x:random(550), _y:random(400)});

ball_array[i].onEnterFrame=function(){
a=Math.random(); //Begin wander code
b=10;
if (a<.5){b=-10}
this._x=this._x-b;
a=Math.random();
b=10;
if (a<.5){b=-10}
this._y=this._y-b;

for (j = 1; j <= 10; j++){ //Begin collision detect code
if (this<>ball_array[j]){
if (ball_array[j].hitTest(this)){setProperty (this, _alpha, 20);}
}//eo this ne ball
}//eo sec loop over j
}//eo enter frame event

ball_array[i].onPress = function(){this.startDrag(true)}; //Drag code
ball_array[i].onRelease = function(){this.stopDrag()};


}//eo primary loop over i