8 Replies Latest reply on May 12, 2011 8:22 AM by HDBurdation

    Selectively playing a movieclip

    HDBurdation

      Hey everybody -

       

      Working on a little game, getting my feet wet with AS2 in Flash 8.  Garden variety space shooter.  What I'm trying to do is show a little explosion movie clip each time the player's ship gets hit by enemy fire.  I thought it would be similar to how my shield powerup works, but apparently it isn't -- or it is, and I'm just doing something wrong.  Here's the code for the shield business, to give an idea of how things are set up.  The shield movieclip is nested inside of the ship movieclip, and it only becomes visible when the ship collides with the shield powerup icon.  It runs for 240 frames, fades out and disappears.  What I'd like to do is have a multi-frame explosion movieclip inside there as well, which appears, plays once, then disappears, whenever the ship is hit.

       

      if (_root.ship.shield._visible == true) {
                      shieldTimer += 1;
                      if (shieldTimer>240) {
                          _root.ship.shield._alpha -= .5;
                          if (_root.ship.shield._alpha<0) {
                              _root.ship.shield._visible = false;
                              _root.ship.shield._alpha = 100;
                              shieldTimer = 0;

       

      I can provide more code, information, etc. if needed.  Any advice would be greatly appreciated.  Thanks!

        • 1. Re: Selectively playing a movieclip
          Ned Murphy Adobe Community Professional & MVP

          If you create that explosion movieclip with a blank first frame and a stop() command in it, then you can just have your code tell that movieclip to play().  When it finishes playing it will loop and stop at frame 1.

          • 2. Re: Selectively playing a movieclip
            HDBurdation Level 1

            Thanks for the response, Ned Murphy.  Sounds simple enough, but I can't seem to get it to work.  My ship timeline is all one frame long.  The layer above the ship's body is the explosion movieclip.  In the explosion clip, the first frame is blank with a stop(); command.  Here's the code within the ship class, under function onEnterFrame(); :

             

            if(this.hitTest(_root.enemyMissile)){
                           
                            gotoAndPlay (_root.ship.shipHit);
                        }

             

            I also tried:

             

            if(this.hitTest(_root.enemyMissile)){
                           
                            gotoAndPlay (_root.ship.shipHit(2));
                        }

             

            I tried extending all the layers in the ship's timeline to 2 frames, putting a stop(); in the first frame of the explosion layer, and telling it to gotoAndPlay(2), but that didn't work either.  I'm obviously doing something horribly wrong, but I have no idea what it is.  Any thoughts?

            • 3. Re: Selectively playing a movieclip
              Ned Murphy Adobe Community Professional & MVP

              You want to tell the movieclip to play, you seem to be teliing the current timeline to gotoAndPlay,  Something along the following lines would be more correct...

               

              if(this.hitTest(_root.enemyMissile)){
                       _root.ship.shipHit.gotoAndPlay(2); // or   _root.ship.shipHit.play();
              }

              • 4. Re: Selectively playing a movieclip
                HDBurdation Level 1

                Still no results.  Should I make it a function, or give the shipHit its own class?  Actually, I tried both of those things last night, and nothing happened.  Of course, there's a good chance I bungled that too.  I also tried putting the hitTest into the enemyMissile class instead, which did nothing.  Is there some more info/code I can post to shed more light on things?

                • 5. Re: Selectively playing a movieclip
                  Ned Murphy Adobe Community Professional & MVP

                  I know I already posted an answer to this, but it is not appearing now, and is not listed in my recent activity, so I'll try one last time (the forums are on their way down the drain)

                   

                  Using gotoANdPlay like that you are telling the timeline you are in to goto whatever... the rest of what follows it is just wrong anyways.

                   

                  You want to tell the animation to play...

                   

                  if(this.hitTest(_root.enemyMissile)){

                       ship.shipHit.gotoAndPlay(2);  // or ship.shipHit.play(); 
                  }

                  • 6. Re: Selectively playing a movieclip
                    Ned Murphy Adobe Community Professional & MVP

                    Now more is suddenly showing after repeating myself...

                     

                    You need to be constantly testing for a hit, using either an enterFrame sort of setup or test each time a position is changed.  If you are just having that conditional by itself, it will only execute once.

                    1 person found this helpful
                    • 7. Re: Selectively playing a movieclip
                      HDBurdation Level 1

                      I'm pretty sure I understand what you're saying, but I still can't get anything to work.  I know it's an error on my end, and it could just be a misplaced bracket or something.  I'll keep at it.  Thanks for the help!

                      • 8. Re: Selectively playing a movieclip
                        HDBurdation Level 1

                        Finally got it working.  I had to go into the enemyMissile class and do this:

                         

                        if(this.hitTest( _root.ship.shipBody ))
                                {
                                    this.removeMovieClip();
                                    if(_root.ship.shield._visible == false)
                                    {
                                    _root.ship.updateHealth(-10);
                                    }
                                    var explosion = _root.attachMovie("shipDamage","shipDamage" + _root.getNextHighestDepth(),_root.getNextHighestDepth());
                                    explosion._x = _root.ship._x;
                                    explosion._y = _root.ship._y;

                                }

                         

                        Ned, thanks again for your help.  Even though I ended up having to do something completely different than what you suggested, it got me thinking about workarounds.