10 Replies Latest reply on Apr 26, 2010 5:46 AM by QZ8

    .hittest not finding target

    QZ8

      i have a game in which the character shoots bullets and kills enemies and whatnot. Now, i am trying to get the bullet to disappear when it collides with a wall. here is the concerned script:

       

      _root['B'+bulletID].onEnterFrame = function() {
        this._x += this.xvel;
        this._y += this.yvel;
        if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {            when it goes out of bounds
         this.removeMovieClip(this);
        }
        if(this.hitTest(_root.wall_mc)) {                                  when it hits the wall ( i am using a trace to test a collision)
        trace("HIT");
        }
        if(this.hitTest(_root.BAD)) {                                 when it hits the enemy
         trace("HIT");
        }
      }

       

       

       

      when the bullet goes out of bounds, the bullet disappears, but when it hits the enemy or wall, it seems that the collision is not detected. I have checked each instance name and linkage, but have so far come up with nothing. any help is appreciated!

        • 1. Re: .hittest not finding target
          kglad Adobe Community Professional & MVP

          linkage is irrelevant to your code.

           

          if you had a movieclip with instance names _root.wall_mc or _root.BAD and your bullet made contact, you would see "HIT" traced.  to check instance names, select the bad guy, and check the properties panel.  BAD should be listed.  in addition, BAD must be on your _root timeline.

           

          p.s.  that should be:  this.removeMovieClip();

          • 2. Re: .hittest not finding target
            QZ8 Level 1

            yes, i am aware that i need this.removeMovieClip();   , but i only used trace so i could see if a collision was detected, as a sort of test. Yes, the instance name is correct (it is listed as BAD). Could you please explain what the _root timeline you mentioned was?

            • 3. Re: .hittest not finding target
              kglad Adobe Community Professional & MVP

              that's the timeline you're referencing in code.  if the swf that contains that code is NOT loaded into another swf, _root is the main timeline.  ie, the one you see if you close your fla and then open your fla.

              • 4. Re: .hittest not finding target
                QZ8 Level 1

                yes, the BAD movie clip is on the _root stage(and so is the wall), but the bullet continues through both without a trace being detected. A trace was detected when the bullet collided with the bounds in set in place (the    if (this._x<0 || this._y<0 || this._x>550 || this._y>400) ), but i need to know why it doesnt work when the bullet collides with objects that are in fact on the _root.timeline. Were you suggesting that i remove linkage on wall_mc and BAD?

                • 5. Re: .hittest not finding target
                  kglad Adobe Community Professional & MVP

                  you don't need to remove any linkage.  it just has nothing to do with your code and makes me suspicious that the instance name of BAD and/or walls is(are) not correct.

                   

                  which movieclips are you creating using attachMovie()?

                   

                  one other thing, if xvel and/or yvel are too large and/or your movieclips too small (in a certain sense), the bullet could pass through a wall or enemy without a positive hittest.

                  • 6. Re: .hittest not finding target
                    QZ8 Level 1

                    i am creating bullets (B) with the attachmovie command so that every time i click, a bullet is shot. The instance names are definitely correct because i just checked them, and i also tried lowering the bullet velocity, but nothing changed.

                    • 7. Re: .hittest not finding target
                      kglad Adobe Community Professional & MVP

                      copy and paste the trace output using:

                       

                       

                      _root['B'+bulletID].onEnterFrame = function() {
                        this._x +=  this.xvel;
                        this._y += this.yvel;
                        if (this._x<0 ||  this._y<0 || this._x>550 || this._y>400) {          

                      trace(_root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xval+" "+this.yvel);

                         this.removeMovieClip();
                        }
                         if(this.hitTest(_root.wall_mc)) {                                 
                         trace("HIT");
                        }
                        if(this.hitTest(_root.BAD))  {                                
                          trace("HIT");
                        }
                      }

                       

                      • 8. Re: .hittest not finding target
                        QZ8 Level 1

                        ok, now the trace is detected, but i need it

                        so the bullet is removed

                         

                        _root['B'+bulletID].onEnterFrame = function() {
                          this._x += this.xvel;
                          this._y += this.yvel;
                          if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                           this.removeMovieClip();
                          }
                          trace(_root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xval+" "+this.yvel);
                        };
                        if (this.hitTest(_root.wall_mc)) {
                          this.removeMovieClip();                <-     how do i make this work?                         
                        }
                        if (this.hitTest(_root.BAD)) {
                          this.removeMovieClip();    <- and this?
                        }

                        • 9. Re: .hittest not finding target
                          kglad Adobe Community Professional & MVP

                          what did the trace reveal?  did you find the problem because of the trace?

                           

                          if so, please mark this thread as answered.

                           

                          at what depths are you adding your bullets?

                          • 10. Re: .hittest not finding target
                            QZ8 Level 1

                            here is the whole code i am working with (on an actions frame on the timeline)

                             

                            onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");                                             this is messing up
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {
                              this.removeMovieClip();
                            }
                            };onMouseDown = function () {
                            var bulletID:Number = Math.random();
                            _root.attachMovie('B', 'B'+bulletID, _root.getNextHighestDepth());
                            _root['B'+bulletID]._x = Hero_mc._x;
                            _root['B'+bulletID]._y = Hero_mc._y;
                            _root['B'+bulletID].xvel = 30*(_xmouse-Hero_mc._x)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].yvel = 30*(_ymouse-Hero_mc._y)/(Math.abs(_xmouse-Hero_mc._x)+Math.abs(_ymouse-Hero_mc._y));
                            _root['B'+bulletID].onEnterFrame = function() {
                              this._x += this.xvel;
                              this._y += this.yvel;
                              if (this._x<0 || this._y<0 || this._x>550 || this._y>400) {
                               this.removeMovieClip();
                              }
                              if(this.hitTest._root.wall_mc+" "+_root.wall_mc._width+" "+_root.wall_mc._height+" : "+_root.BAD+" "+_root.BAD._width+" "+_root.BAD._height+" "+this.xvel+" "+this.yvel);
                                 trace("HIT");
                            };
                            if (this.hitTest(_root.wall_mc._width+" "+_root.wall_mc._height+" "+this.xvel+" "+this.yvel)) {                    Trying to get this to work
                              this.removeMovieClip();
                            }
                            if (this.hitTest(_root.BAD)) {                             and this
                              this.removeMovieClip();
                            }
                            };

                             

                            For some reason, a "HIT" is now being detected no matter where i shoot the bullet. I need to limit that "HIT" area to the wall/enemy.