PDA

View Full Version : Can't get linkage to movie clip from within another in for loop working


GreenGecko
12-06-2007, 03:02 PM
Hello,

I'm trying to set up hit detection for some enemy move clips in a shooting game but am having a lot of trouble with the syntax for linkage between the AS-containing aiming reticule and the instances of the enemy movie clip... I'd really appreciate any advice since I need to have this working ASAP so I can work on the art..

What I have at the moment that works:

I have a spawnpoint, mc_spawn_madman, that spawns insances of movie clip mc_madman using setInterval calling a function that uses attachMovie to pull instances of mc_madman out of the library:

onClipEvent (load) {

spawn_speed = 2500;
number_enemies = 15;
instance=0;

setInterval(this,"spawnMadman",spawn_speed);

function spawnMadman() {
instance++;
if(instance <= number_enemies){
this.attachMovie("mc_madman", "mc_madman"+instance, this.getNextHighestDepth());
}
}

}

mc_madman contains another movie clip, mc_madman_image, to contain its animation. The fourth frame will start a death animation (at the moment a single frame). On the mc_madman level are three frames containing the following AS to control movement, and a transform for when the movie clip moves left (taken from a guide and adapted for my purposes):

Frame 1:

loops = 0;
target_x = (Math.random()*400) - 150;
target_y = (Math.random()*280) + 40;
x_div = (target_x - mc_madman_image._x)/45;
y_div = (target_y - mc_madman_image._y)/45;

position_x_initial = mc_madman_image._x;

Frame 2:

loops++;
mc_madman_image._x += x_div;
mc_madman_image._y += y_div;

position_x_now = mc_madman_image._x;

if(position_x_initial > position_x_now) {
mc_madman_image._xscale = -30;
} else {
mc_madman_image._xscale = 30;
}

Frame 3:

loops++;
mc_madman_image._x += x_div;
mc_madman_image._y += y_div;

if (loops < 45) {
gotoAndPlay(2);
} else {
gotoAndPlay(1);
}


Now the problem is I think with this code, the code on the reticule, mc_reticule, that detects hits. mc_reticule is contained within mc_gun, which includes a gun drawing. Code:

onClipEvent(mouseDown){

for(instance=0; instance<=15; instance++) {
if(this.hitTest("_root.mc_spawn_madman.mc_madman"+instance)) {
trace("hit!");
}
}

}

Now as you can see, I'm using trace("hit!") to debug, and this works completely fine, it detects a hit, and one hit only, on mousedown and collision with any of the mc_madman instances (mc_madman1 though 15 as determined by the loop in the mc_spawn_madman script). So it is not that if statement that is the problem. What I absolutely cannot get working is something like, _root.mc_spawn_madman.mc_madman + instance.gotoAndPlay(4); to trigger the death animation . I have also tried _root["mc_spawn_madman.mc_madman"+instance].gotoAndPlay(4); or something to that effect.

I can't seem to get this linkage working at all.

Does anyone have any ideas for correct syntax, or anything that might be wrong that I can try...

Thanks muchly :D,

Massive AS2 N00b # 1242

rrh
12-06-2007, 04:16 PM
Have you tried
_root.mc_spawn_madman["mc_madman"+instance].gotoAndPlay(4);

GreenGecko
12-06-2007, 04:29 PM
MASSIVE WOOTAGE!!

Thank you so much, my assignment is due next Friday, now that this is sorted I have a spawning engine that I can use to construct the spawning of different enemies from different places, with unique attributes and score values, etc. Whooooooooooooooooo!

(I was taclking this for 12 hours!!)

Yay.

Thanks again. :D