1 Reply Latest reply on Aug 4, 2006 3:13 AM by Newsgroup_User

    hitTest not working as it should

    Heros_Darkness
      Hello, I am currently working on a game as a project for my website. It's style is based after asteroids with a few similar and different features. I have most of the scripting done, but I am having problems with one part. I need help with my hitTest with my bullets. All my actionscript is on the main timeline and is together. It all works except the part in bold, which is the bullet hitTests.
      quote:


      stop();
      //Bullet scripts
      ["Bullet"+Num]onEnterFrame = function() {
      if(enemy.hitTest(this)){
      enemy._x-=40;
      enemy._y-=40;
      }

      }
      //enemy scripts
      enemy.onEnterFrame = function() {
      if(this._x<hero._x){
      this._x+=2;
      }
      if(this._x>hero._x){
      this._x-=2;
      }
      if(this._y<hero._y){
      this._y+=2;
      }
      if(this._y>hero._y){
      this._y-=2;
      }
      }
      //hero scripts
      Num = 0;
      hero.onEnterFrame = function() {
      //movement script
      if(Key.isDown(Key.LEFT) || Key.isDown(65)){
      this._x-=4;
      }
      if(Key.isDown(Key.RIGHT) || Key.isDown(68)){
      this._x+=4;
      }
      if(Key.isDown(Key.UP) || Key.isDown(87)){
      this._y-=4;
      }
      if(Key.isDown(Key.DOWN) || Key.isDown(83)){
      this._y+=4;
      }
      //angle script
      var x:Number = _xmouse-this._x;
      var y:Number = _ymouse-this._y;
      var angleRad:Number = Math.atan2(y, x);
      var angleDeg:Number = angleRad/Math.PI*180;
      this._rotation = angleDeg;
      };
      ShootBullet = function () {
      var Bullet = _root.attachMovie("Bullet", "Bullet"+Num, _root.getNextHighestDepth());
      var point = {x:this.hero.gunTip._x,y:this.hero.gunTip.
      _y};
      this.hero.localToGlobal(point);
      Bullet._x = point.x;
      Bullet._y = point.y;
      Bullet._rotation = _root.hero._rotation;
      Bullet.onEnterFrame = function() {
      Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*2
      5;
      Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*2
      5;
      };
      Num++;
      };
      hero.onMouseDown = function() {
      ShootBullet();
      };


      The problem here is that it is doing the
      enemy._x-=40;
      enemy._y-=40;
      part from the very start. So as the movie/game starts the enemy is already doing the actions as though the bullet was covering the enemy at all times.
      For the fla. file, click here.
      To view the swf, click here
      You can also find out how I created the file in 7 easy steps here.
        • 1. Re: hitTest not working as it should
          Level 7
          hitTest works fine, it's the *developer* that's broken ;)

          What you are trying to do there is a bit confused. This:

          ["Bullet"+Num]onEnterFrame = function() {
          if(enemy.hitTest(this)){
          enemy._x-=40;
          enemy._y-=40;
          }

          has several problems. It should be:

          this["Bullet"+Num].onEnterFrame = function() {
          if(this._parent.enemy.hitTest(this)){
          this._parent.enemy._x-=40;
          this._parent.enemy._y-=40;
          }

          **BUT** you are already assigning each bullet an enterFrame handler to
          create it's moviement. Doing this will replace that handler, as any object
          can only have a single enterFrame handler at a time. So, this works:

          ShootBullet = function () {
          var Bullet = _root.attachMovie("Bullet", "Bullet"+Num,
          _root.getNextHighestDepth());
          var point = {x:this.hero.gunTip._x, y:this.hero.gunTip._y};
          this.hero.localToGlobal(point);
          Bullet._x = point.x;
          Bullet._y = point.y;
          Bullet._rotation = _root.hero._rotation;
          Bullet.onEnterFrame = function() {
          Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*25;
          Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*25;
          if (Bullet._parent.enemy.hitTest(Bullet)) {
          Bullet._parent.enemy._x -= 40;
          Bullet._parent.enemy._y -= 40;
          }
          };
          Num++;
          };



          You are also going to need to create code to remove each bullet once it has
          hit the enemy or left the screen, or else your game will rapidly slow to an
          unplayeable crawl...

          ShootBullet = function () {
          var Bullet = _root.attachMovie("Bullet", "Bullet"+Num,
          _root.getNextHighestDepth());
          var point = {x:this.hero.gunTip._x, y:this.hero.gunTip._y};
          this.hero.localToGlobal(point);
          Bullet._x = point.x;
          Bullet._y = point.y;
          Bullet._rotation = _root.hero._rotation;
          Bullet.onEnterFrame = function() {
          Bullet._x += Math.cos(Bullet._rotation*(Math.PI/180))*25;
          Bullet._y += Math.sin(Bullet._rotation*(Math.PI/180))*25;
          if (Bullet._parent.enemy.hitTest(Bullet)) {
          Bullet._parent.enemy._x -= 40;
          Bullet._parent.enemy._y -= 40;
          removeMovieClip(Bullet);
          }
          if (Bullet._x<0 || Bullet._x>Stage.width || Bullet._y<0 ||
          Bullet._y>Stage.height) {
          removeMovieClip(Bullet);
          }
          };
          Num++;
          };


          In addition, instead of
          Bullet._parent.enemy._x -= 40;
          Bullet._parent.enemy._y -= 40;

          You need to move the enemy in relation to the bullet's vector. Otherwise, if
          i fly to the top left of the enemy then shoot it, it bounces in to me....
          --
          -------------------------------
          Remove '_spamkiller_' to mail
          -------------------------------