4 Replies Latest reply on Apr 8, 2008 8:23 AM by Rothrock

    hit Test

    FlashMonkey666 Level 1
      Hi, I'm making a game where you navigate a maze.

      I'm using hit test to check if you are inside of a "wall" object, which makes the player stop. But since the maze is so big, I don't want to have to do a hit test for every single wall. Is there a way to to do a hit test for a whole object, but like only the black lines in that object? In other words, can the "maze" object be an irregular shape, not a rectangle?

      function wallcheck(direction) {
      //direction 1=R, -1=L
      var ycord = (_root.player.y);
      if (direction == 1) {
      var xcord = (_root.player.x+(_root.speed));
      } else {
      var xcord = (_root.player.x-(_root.speed));
      var wallvar = "maze";
      if (_root[wallvar].hitTest(xcord, ycord)) {
      return "yes";
      //this exits the function
      } else {
      return "no";
        • 1. Re: hit Test
          FlashMonkey666 Level 1
          I guess what I'm just asking here is if you can do a hit test with an IRREGULAR-shaped object.

          • 2. Re: hit Test
            rlc5611 Level 1
            You can find one here:

            hit test

            Although kglad advertises on his web site that he has a better one.
            • 3. Re: hit Test
              I think you need to add the 3rd parameter that determines if you are checking against the hit Target's bounding box, or hit area. You could define a movieclip within your maze that is just the hit area... depends on how complex it is. Say your hitTest shape inside the maze clip is called 'hitDetector':

              var wallvar = "_root.maze.hitDectector";
              if (wallvar.hitTest(xcord, ycord, true)) {

              Another trick I learned is if your player is larger than 1 pixel (which they usually are), it is nice to detect when an area around the character sprite is hitting the wall. Inside the character sprite, create a few points (empty movie clips), then have each detect a hit from its x,y coordinates... basically you can create a box, circle or spaceship shape hit detection around each object in your game. I can find some code for this if you require a spae around your character for hit detection.
              • 4. Re: hit Test
                Rothrock Level 5
                There are many tricky ways to do this and most of the relate to how you organize your "walls" or whatever it is checking that you hit. For example if each of your walls is a separate clip with the same parent as your "player" you are going to have to check each wall. So let's say there are 16 walls in the neighborhood, that would be 16 tests every single frame, even if there isn't a hit you will have to do 16 tests. But if you put all the walls into a parent clip then as a first pass you can just check the parent and if there is no hit you are done -- just one test. Of course if there is a hit with the parent you will most likely need to know specifically which one was hit. Again you could be clever about how you attached the walls. Perhaps if you placed walls into ahead, behind, left, right clips (or some such) then 4 more tests and maybe four more after that. In that case you've gone from a worst case scenario of 16 tests to 9 and a best case scenario of 16 to 1! And I'm sure there are even more clever ways to deal with the worst case scenario.

                But here I'm just talking about culling down the tests that need to be performed. There are also many different ways to test. I've used gskinners collision detection and it is nice and simple. I don't know about kglad's version, but if he claims it is faster I would believe him. But the real fastness comes from only testing what needs to be tested.

                Check out polygonal.de if you want to find out more about collision detection than you ever wanted to know.