Monday, December 28, 2009

How do I do multiple collision tests in ActionScript/Flash?

With some difficulty I can get a collision test for one object in ActionScript, but if I give another object the same name and try to collide with it, it just ignores it. Do I seriously have to put new names and redo the code for each one?





I tried putting the code in each object that was going to be collided WITH, but I can't seem to control the moving object (ball) with something like ';ball._x + 1'; or something.How do I do multiple collision tests in ActionScript/Flash?
AS2 or AS3 (Actionscript 2 or Acitonscript 3)?





Objects cannot have the same instance name. They are unique. I re-read your post again and again but im not sure exactly what your trying to do.





1. if you want all balls to collide with one object, then make each ball


hitTest for that object





//make a ball class and give it the object you want to test





class Ball


{


var objectToHit:MovieClip;





public function Ball(objectToHit:MovieClip)


{


this.objectToHit = objectToHit;


//make ';this'; hitTest for ';objectToHit'; in AS2 or AS3


}


}





2. if you want all balls to collide with each other, then create an array of balls.





var balls:Array = new Array();


balls.push(new Ball());


for(var i:Number = 0; i %26lt; 10; i++)


{


for(var j:Number = 0; j %26lt; 10; j++)


//make balls[i] hitTest for balls[i] in AS2 or AS3


}

No comments:

Post a Comment