Let's start by showing you some ways to be DRY (Don't Repeat Yourself)
First, do yourself a favor and learn how to use Arrays. Arrays are a way to store collections of things. Let's make an array for your missiles, and one for your invaders.
var missles:Array = [missile1, missile2, missile3, missile4]; //populate this with your missiles, even DRYer would be to create these missiles in a for-loop programatically
var invaders:Array = [invader1, invader2, invader3, invader4];
Now, in your enter frame handler, loop through each item in the missile array:
addEventListener(Event.ENTER_FRAME, moveMissile);
function moveMissile(e:Event):void {
//arrays are 0 based, which means the first item is accessed with missles[0]
//iterate backwards if you are going to potentially remove something from the array during the loop
//so if there were 4 missiles, we iterate backwards from 3 to 0
//before the loop, we give it a label: MissileLoop: , this gives you a way to exit the missile loop early if needed
MissileLoop: for(var i:int=missiles.length-1;i>=0;i--){
//move the missile
missiles[i].y += 30;
//iterate through each invader to check for a hit
//iterate backwards so if you remove an item it doesn't throw off the iterator (j)
for(var j:int=invaders.length-1;j>=0;j--){
if(missile[i].hitTestObject(invaders[j])){
//play the death animation
invaders[j].gotoAndPlay("Invader Death");
//if there was a hit, remove the missile from the array
//so it doesn't keep checking for hits on subsequent frame ticks
invaders.removeAt(j);
//you may want to also remove the missile (if it can only destroy one invader)
missiles[i].gotoAndPlay("Missile Explodes");
missiles.removeAt(i);
//this tells the outer loop which you labelled 'MissileLoop' to move on to the next item early (so we don't keep checking hittests with the other invaders)
//only do this if you destroy the missile, if missiles can not be destroyed, omit this part
continue MissileLoop; //basically this says don't keep looping through the other invaders, as the missile is destroyed
}
}
}
}
Now that you've done this, you need take the advice of @Kyle's answer and add a frame script at the end of the your death animation.
So on the last frame of your invader death animation, add the following script:
this.visible = false;
That will hide the invader, and since you already removed it from the array it won't be checked for missile hits any longer.
Now that your DRYer, you can also add more missiles or invaders by simply adding more to your arrays (no extra code needed).
As mentioned in the comments, you error is because of this line:
addEventListener(Event(root["missile"].hitTestObject(invader)));
AddEventListener expects a string as it's first argument, and a callback function as the second. You are attempting to give it a boolean (true/false) value converted to an Event - which isn't possible.
If (to more directly answer your question) you wanted to delay an action, you would use flash.utils.setTimeout or a Timer.
For example:
if(root["missile"+i].hitTestObject(invader4)){
invader4.gotoAndStop("Invader Dead");
//call the function hideInvader 2 seconds (2000 milliseconds) from now, and pass the parameter invader4 to it
flash.utils.setTimeout(hideInvader, 2000, invader4);
}
function hideInvader(invader:DisplayObject):void {
invader4.x = 200000;
}
That example said, setTimeout is best avoided as it's easy to get sloppy and inefficient with it. You are much better off with my first example and following Kyle's answer.
addEventListener(Event(root["missile"].hitTestObject(invader)));
. This is not how you use Events. – user2655904