5 Replies Latest reply on Nov 25, 2006 4:03 PM by yuvic

    hitTest not working

    mpwalker
      Hi all, I'm trying to build a simple adventure game engine and I'm haveing problems getting the boundry hitTests from Ed Mack's tutorial to work. I'm pretty new to actionscript and if someone wouldn't mind taking a look at my code and telling me what I'm doing wrong here, I'd appreciate it. You can download the .fla Here. He's supposed to stay in the maze.Thanks.
        • 1. Re: hitTest not working
          tiago.braga
          Please, put your code here, so we can help you... its note good download files... it takes so much time.

          • 2. Re: hitTest not working
            mpwalker Level 1
            Ok here's the code:

            On character MC:
            onClipEvent(load){
            s = 2;
            b = this.getBounds(this);

            function move(x,y){
            if(!_root.hit.hitTest(_x+x+b.xmin+1,_y+y+b.ymin+1,true)){
            if(!_root.hit.hitTest(_x+x+b.xmax-1,_y+y+b.ymin+1,true)){
            if(!_root.hit.hitTest(_x+x+b.xmin+1,_y+y+b.ymax-1,true)){
            if(!_root.hit.hitTest(_x+x+b.xmax-1,_y+y+b.ymax-1,true)){
            _x += x;
            _y += y;
            }
            }
            }
            }
            }
            }

            onClipEvent(enterFrame){
            if(Key.isDown(Key.UP)){
            move(0,-s);
            }
            if(Key.isDown(Key.DOWN)){
            move(0,s);
            }
            if(Key.isDown(Key.LEFT)){
            move(-s,0);
            }
            if(Key.isDown(Key.RIGHT)){
            move(s,0);
            }
            }
            And in frame one to have the virtual camera follow the character MC:
            moveI=setInterval(moveF,0);
            function moveF(){
            camFrame._x=hero._x;
            camFrame._y=hero._y;
            updateAfterEvent();
            }

            The reason I didn't post the code and ask you to download the file instead is that I created two almost identical files, with the same hitTest code to keep the Character MC in the boundaries. But one file isn't working and the other one is. I can't figure it out and I posted the file that wasn't working. If you download it, I think you'll get a better idea of what I'm trying to do.
            • 3. Re: hitTest not working
              mpwalker Level 1
              anyone?
              • 4. Re: hitTest not working
                tiago.braga Level 1
                Try to change xmin to xMin first.
                in my test, trace(b.xmin) returns NaN and trace(b.xMin) returns a real number.
                • 5. Re: hitTest not working
                  yuvic
                  okay, in your original FLA file that you posted replace the code on the blank frame with this:
                  n = 0;
                  onEnterFrame = function () {
                  if (_root.hit.hitTest(_root.hero)) {
                  trace("ball intersects square for the "+n+" time!!!");
                  }
                  n++;
                  };

                  Your mistake was - you called the maze hitme in the hittest code instead of hit - which is its instance name. This basic mistakes will go away with practice, so don't worry about it too much.
                  The new code I wrote checks if the hitTest every frame entrace (if your frame rate is 80 - so it is 80 times a second) and adds a number to it to the input box so you see when it changes and when it stops.
                  Good luck!
                  Yuval