0 Replies Latest reply on Feb 4, 2009 7:46 AM by jiggy1965

    For Loop and Hittest problem

    jiggy1965 Level 1
      I've uploaded a file which makes a ball move to the left when pressing the right arrow key.
      Download Fla File

      this.onEnterFrame = function() {
      if (Key.isDown(Key.RIGHT)) {
      for (i=0; i<=200; i++) {
      if (!ball.hitTest(100, 50, true)) {

      On a Key.RIGHT it goes through a for loop. Shifting the ball 200 times 1 pixel to the left. Every iteration it tests for a hit with coordinates 100/50 (indicated by the red crosshairs). If there's no hit it moves 1 pixel to the left. If there is a hit it stops moving (no ball._x--).

      Funny thing is: while it eventually finds a hit, it does a final For Loop. Even though it should find a hit during that last loop when the ball is passing the crosshairs, it doesn't and moves the ball a final time 200 times 1 pixel to the left. Ending up with below image.

      View Image

      Why doesn't it find a 'hit' for that final For Loop while it's clearly passing it? :