3 Replies Latest reply on Sep 16, 2009 9:33 PM by kglad

    attachmovie, hitTest & multidimentional array question...

    christoon_ca

      Hi all

       

      I'm trying to make a game (my first) and I'm having trouble making my dynamically attached bullets and enemies collide. I've tried so many variations, tutorials, forum searches... to no avail.

       

      Here's my code:

       

      bulletArr = new Array();
      var bulletCount = 0;
      
      badArr = new Array();
      var badCount = 0;
      var badAmount = 0;
      
      onEnterFrame = function() {
          if (Key.isDown(Key.SPACE)) {
              bulletCount ++; 
              var bulletName = bulletArr.push("bullet" + bulletCount);
              attachMovie("bullet", bulletName, bulletCount)
              this[bulletName]._y = random(Stage.height);
          }
          badAmount ++;
          if (badAmount > 50) {
              badCount ++;
              var badName = badArr.push("bad" + badCount);
              attachMovie("bad", badName, badCount);
              this[badName]._x = random(Stage.width);
              this[badName]._y = random(Stage.height);
              badAmount = 0;
          }
          this[bulletName].onEnterFrame = function() {
              this._x += 10;
              if (this._x > 560) {
                  this.removeMovieClip();
              }
              for (i=0; i<badArr.length; i++) {
                 if (this.hitTest(badArr[i])) {
                      badArr[i]._alpha -= 50;
                  }
              }
          }
      }
      

       

      Any help is appreciated.

      Thanks

        • 1. Re: attachmovie, hitTest & multidimentional array question...
          kglad Adobe Community Professional & MVP

          badArr is an array of strings.  you need to help flash resolve those strings to movieclips using array notation  or, even easier just add the movieclip references to your arrays:

           


           

          bulletArr = new Array();
          var bulletCount = 0;
          
          badArr = new Array();
          var badCount = 0;
          var badAmount = 0;
          
          onEnterFrame = function() {
              if (Key.isDown(Key.SPACE)) {
                  bulletCount ++; 
                  var bulletName = "bullet" + bulletCount;
                  var bmc:MovieClip = attachMovie("bullet", bulletName, bulletCount)
                  bulletArr.push(mc);
                  bmc._y = random(Stage.height);     }     badAmount ++;     if (badAmount > 50) {         badCount ++;         var badName = "bad" + badCount;         var mc:MovieClip = attachMovie("bad", badName, badCount);         badArr.push(mc);
                  mc._x = random(Stage.width);         mc._y = random(Stage.height);         badAmount = 0;     }     bmc.onEnterFrame = function() {         this._x += 10;         if (this._x > 560) {             this.removeMovieClip();         }         for (i=0; i<badArr.length; i++) {            if (this.hitTest(badArr[i])) {                 badArr[i]._alpha -= 50;                 if(badArr[i]._alpha<=0){
                             var mc:MovieClip = badArr[i];
                             badArr.splice(i,1);
                             mc.removeMovieClip();
                          }
                      }         }     } }

           

          Any help is appreciated.

          Thanks

          • 2. Re: attachmovie, hitTest & multidimentional array question...
            christoon_ca Level 1

            Thank you soooooo much, I was pulling out my hair...

             

            I'm really curious, do you help people here for the joy of teaching, or as a job? I don't mean to be rude/forward. Whatever the reason, it is very much appreciated.

             

            Thanks

            • 3. Re: attachmovie, hitTest & multidimentional array question...
              kglad Adobe Community Professional & MVP

              you're welcome.

               

              no, i'm not paid for helping people.  but i do get hired occassionally by people i help.