3 Replies Latest reply on Oct 28, 2007 7:04 AM by clbeech

    Hit Test Issue

    Anim9or
      Hi, sorry to bother you again, but I'm working on a new project for school, a history game. However, i am starting with simple scoring. I'm experimenting with hit tests and I keep running accross the asame problem, I try to get the score to rise when the circle passes the square, but it doesnt seem to respond, it works with drag and drop, but not keyboard controls.
      Here's teh action script, don't be surprised if it looks familiar since it's a tweaked code I got from a maze tutorial:
      onClipEvent (enterFrame) {
      with (_root.circle_mc) {

      // Controls circle_mc Speed
      mySpeed = 5;

      // keyboard controls
      if (Key.isDown(Key.DOWN)) {
      _y += mySpeed;
      }
      if (Key.isDown(Key.UP)) {
      _y -= mySpeed;
      }
      if (Key.isDown(Key.LEFT)) {
      _x -= mySpeed;
      }
      if (Key.isDown(Key.RIGHT)) {
      _x += mySpeed;
      }

      }

      // detect if Maze is finished
      if (_root.square_mc.hitTest(_x, getBounds(_root).yMax, true)) {
      score = score+1;
      } else {
      score = score;
      }
      }


      circle_mc is the circle controlled byt the keyboard
      square_mc is the square it's supposed to pass
      and score is the variable for the scoring dynamic text

      Let me know if there's any more info you need!
        • 1. Hit Test Issue
          clbeech Level 3
          there are several things here that you may want to do differently. First, I would place this code on the main timeline in frame1. Second, when using a shapeflag based hitTest method, you are comparing a single point, to a given MC instance, but the coordinates used must be within the same scope. So in the code above, you are comparing the _x position point of circle_mc and the yMax position point of circle_mc which would result in being a point at the lower left corner of the bounding box. If you are only trying to compare weather one shape's bounds comes in contact with another, then use the movieclip as a target.

          So I would write this code as follows, on the first frame of the main timeline, with instances of 'circle_mc' and 'square_mc':

          • 2. Re: Hit Test Issue
            Anim9or Level 1
            Sorry if this is important, but I neglected to mention that I am using Flash MX and only AS2, will this code still work, i am having trouble with the new keyboard controls.
            • 3. Re: Hit Test Issue
              clbeech Level 3
              yes this is AS2 code. In this example instead of using a series of IF statements, I consolidated them into a 'switch' statement. When a Key is press it check for the Key code, then compares it using the 'case' statements.

              PS. after the last IF statment in the block, you should assign the current value of 'score' to the textField instance you have set up as in:

              myScore_txt.text = score;