3 Replies Latest reply on Sep 18, 2012 7:10 AM by Ned Murphy

    How to calculate if an object is between two points

    LukeElChul Level 1

      ok so I am making a RTS game and ive got my drawBox

       

      dragging = false;

      checkit = false;

      startPt = {};

      endPt = {};

      var i = 0;

      fill._alpha = 0.1;

       

       

      onMouseDown = function() {

           dragging = true;

           startPt.x = _xmouse;

           startPt.y = _ymouse;

      }

       

       

      onMouseUp = function() {

           dragging = false;

           checkit = true;

           endPt.x = _xmouse;

           endPt.y = _ymouse;

      var square_mc = this.createEmptyMovieClip(""+i, i++);

      square_mc.beginFill(fill);

      square_mc.moveTo(startPt.x, startPt.y);

      square_mc.lineStyle(4, 00000000, 50);

      square_mc.lineTo(_root._xmouse, startPt.y);

      square_mc.lineTo(_root._xmouse, _root._ymouse);

      square_mc.lineTo(startPt.x, _root._ymouse);

      square_mc.lineTo(startPt.x, startPt.y);

      square_mc.endFill();

      square_mc = null;

      }

       

       

      onEnterFrame = function() {

           if (!dragging and checkit) {

                // cycle through objects and see if they are between startPt and endPt

                checkit = false;

           }

      }

       

      And i want to check if my workers are between the two points ( well 4 points ) of:

       

      startPt.x

      startPt.y

      endPt.x

      endPt.y

       

      Ive tried this

       

      if(_root.workers[i]._x > startPt.x && _root.workers[i]._y > startPt.y && _root.workers[i]._x > endPt.x && _root.workers[i]._y > endPt.y || _root.workers[i]._x < startPt.x && _root.workers[i]._y < startPt.y && _root.workers[i]._x > endPt.x && _root.workers[i]._y > endPt.y){

                                                        workers[i].Selected = true;

                                                        workers[i].gotoFunction();

                                              }

       

      But i dont think thats actualy calculating if the worker[i] is between these points.

       

      so could someone please tell me how to calculate if my worker is between these points, so i could then make it selectable

       

      Thanks in advance