Collision detection?

Hi ,i have recently recieved what i would call inspiration for my next project , just i dont have much experience in the area . I need to detect collisions betweeen duplications of two movieclips , one that is duplicated my user input , the other is duplicated randomly , however , as the names of these objects change , i do not know how to detect collisions between the two , so that they react , or remove, correctly . The code i am using for duplication is as follows :

For object 1 :

if (Key.isDown(Key.SPACE)) {
duplicateMovieClip(“_root.attack”, “bullet”+i, i);
i++;
}

For object 2 :

if (random(64) == 0) {
duplicateMovieClip(enemy, “enemy” + i, i);
i++;
}

As can be seen by this code , each of the objects is duplicated with a unique name , how can i detect collisions between any pair of the different movieclips ?

thnx

I can’t remember exactly the code for collision detection, but I know the variable used is called “hittest”. This is Actionscripts very own collision detector and works just as it should do.

Hopefully someone on here can clarify this for me ?

Well i spent some time on this and I cant figure out how to optimize my code. This bugger gets incredibly laggy quick.
Explanation of code:
the hitTest code is stored inside the little blue rectangle.
The code for duplicating movies are stored in frame 1. Everytime space bar is held down more “bullets” are duplicated. The “enemy” red circle is duplicated when random hits the right number and then its stored into an array. The array’s index increases along with the duplication. The hitTest on the rectangle cyles through each enemy to see if it actually hit. The number in the middle will decrease as enemies get hit.

Can someone optimize my code and tell me how to make it less laggy? its really buggin me out.

Important: the for loop on the rectangle movie clip plays a big role in the lag. Decrease the condition for better performance, however this will cap the number of enemies that can be hit. i.e it doesnt hit after the 10th enemy if condition is set to i<10.

there is no way to go about this efficiently… your best bet would probably to use an array and then in the onEnterFrame actions of one set of clips (probably the user set) loop through the array:


enemies_arr = new Array();
//duplicate second clip
if(random stuff) {
	duplicateMovie(enemy, "enemy" + i, i);
	_root.enemies_arr.push(i);
	i++;
}
//actions for hittest
for(k=0;k<_root.enemies_arr.length;k++) {
	if(this.hitTest(_root["enemy"+_root.enemies_arr[k]]) {
		_root.enemies_arr.splice(k, 1); //deletes clip from array
		//hit actions here
	}
}

So what happens is it loops through the length of the array (which contains the changing variable of the name) and then uses the value in the array to check each clip seperatley. If it finds a match it removes the index from the array. This will work, however it will become inefficient in the event of a large number of clips, whether they are in the array or if they are looping through.

I hope that makes sense. :slight_smile:

What you can do is modify your game to recycle index values or remove movie clips when they are not used, that way your system is always updating itself in a way.