2
votes

instead of instantly disappearing after if statement is complete i would like it to play death animation frames first how could i do this? this is my code atm.

    addEventListener(Event.ENTER_FRAME, moveMissile);
function moveMissile(evt:Event) {
   for (var i=1; i<=missileCount; i++) {
      root["missile"+i].y+=-30;
      if (root["missile"+i].hitTestObject(invader)) {
          invader.gotoAndStop("Invader Dead");
      }
      if (root["missile"+i].hitTestObject(invader2)) {
          invader2.gotoAndStop("Invader Dead");
          invader2.x=200000
      }
      if (root["missile"+i].hitTestObject(invader3)) {
          invader3.gotoAndStop("Invader Dead");
          invader3.x=200000
      }
      if (root["missile"+i].hitTestObject(invader4)) {
          invader4.gotoAndStop("Invader Dead");
          invader4.x=200000
      }
   }
}   
var myStartTime = getTimer();
var requiredTimeSeconds = 5;
addEventListener(Event(root["missile"].hitTestObject(invader)));
function playGame(e:Event) {
    var tempTime = getTimer() - myStartTime;
    if (tempTime  >  requiredTimeSeconds * 1000) {
        invader.x=200000
    }   
}       
stop();

and these are the errors i'm receiving "Scene 1, Layer 'Actions', Frame 1, Line 164, Column 18 1136: Incorrect number of arguments. Expected 2.", "Scene 1, Layer 'Actions', Frame 1, Line 164, Column 18 1067: Implicit coercion of a value of type flash.events:Event to an unrelated type String." any help would be appreciated!"

2
Your error comes from this line addEventListener(Event(root["missile"].hitTestObject(invader)));. This is not how you use Events.user2655904
@lukeet - did you find a solution?BadFeelingAboutThis

2 Answers

2
votes

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.

1
votes

I'm guessing Invader Dead is the death animation frame label and you're setting x to 200000 to make the enemies disappear. If that's the case, don't set x to 200000 right away. Include another frame script at the end of the death animation that makes the invader disappear.