PDA

View Full Version : target duplicated mc's



mjward81
March 30th, 2005, 09:53 PM
(bare with me only venturing into intermediate AS)

OK! building a spaceship game where you have to blow up enemy fighters. The enemy fighters are duplicated off a mc with the instance name of "enemy1". The following code is used


for (i=2; i<=numEnemy; i++) {
L1.enemy1.duplicateMovieClip("enemy"+i, i+10);
}

All is working ok, hitTests work great, BUT i want to have it so that each fighter must be hit by lets say 4 bullets before exploding. More realistic.

I read duplicated movieclips dont store variables from their parents, and it has me confused on how to accomplish it. I thought of storing variables on _root in relation to the max enemy fighters i have at one time, then adding a point on each time a bullet hit the enemy. Once hits 4, explode enemy, and reset variable. But i cant seem to work out how to target the duplicated moveiclip.

Can someone please help, ive been looking at it so long im even starting to confuse myself

Lomadrian
March 30th, 2005, 10:17 PM
Here's how I would do it:


for (var i = 2; i<=numEnemy; i++) {
var newEnemy = L1.enemy1.duplicateMovieClip("enemy"+i, i+10);
newEnemy.health = 4;
newEnemy.hurt = function() {
this.health--;
if (this.health == 0) {
// Insert code to kill the enemy
}
};
}

In order for this to work, just call the enemy's hurt function whenever he's hit by a bullet.

Hope this helps.

mjward81
March 31st, 2005, 12:09 AM
sort of... have been trying to dissect code for a little bit and have a few questions


for (var i = 2; i<=numEnemy; i++) {
var newEnemy = L1.enemy1.duplicateMovieClip("enemy"+i, i+10);
newEnemy.health = 4;
newEnemy.hurt = function() {
this.health--;
if (this.health == 0) {
// Insert code to kill the enemy
}
};
}

When you say insert code here to kill enemy, what am i actually targetting? L1["enemy"+i]? How will it know what enemy to kill.

I dont understand for statements very well, i thought i was a variable that changed, so wouldnt by targetting 'L1["enemy"+i]' destroy the last duplicated enemy?