10 Replies Latest reply on Oct 13, 2012 4:18 PM by alaStickman

    Follow an object (mouse click) without decreasing the speed

    alaStickman Level 1

      hello,

      i want to make a movie clip follow the mouse while we are pressing the left Button (NOT "be" the mouse ), i end it up with this code but am having a problem,

      -the speed of the movie clip decrase by his own, i know that this is happening because of the last two lines, i will be glad if anyone could help me to solve this problem, thanx

       

       

      onClipEvent (enterFrame) {

       

       

                onMouseUp = function ()

                {

                          apui = false;

                };

                onMouseDown = function ()

                {

                          apui = true;

                          rotateSpeed = 3;

                };

                if (apui == true)

                {

                          goX = _root._xmouse;

                          goY = _root._ymouse;

               

      }

                else if (apui == false)

                {

                          goX = this._x;

                          goY = this._y;

       

       

                }

       

                this._x -= (this._x - this.goX)/20 ;

                this._y -= (this._y - this.goY)/20 ;

      }

        • 1. Re: Follow an object (mouse click) without decreasing the speed
          kglad Adobe Community Professional & MVP

          if you don't want that movieclip to ease into the mouse position, what do you want it to do?

          • 2. Re: Follow an object (mouse click) without decreasing the speed
            alaStickman Level 1

            no i want that but the thin is the speed is not constant, i want to found a solution for that 

            thank you

            • 3. Re: Follow an object (mouse click) without decreasing the speed
              kglad Adobe Community Professional & MVP

              that's because you're EASing the movieclip into the mouse position.  if you don't do that you must choose some other way to move the movieclip to your mouse position.

               

              stating you want the movieclip to move at a constant speed is not enough information to determine the movieclip's movement.  how long between the start of mouse movement and the start of the movieclip's movement?  how long should it take for the movieclip to reach the mouse position?

              • 4. Re: Follow an object (mouse click) without decreasing the speed
                alaStickman Level 1

                ah sorry then, i don't want to "ease" , here is what i want to do ,

                when we click somewher in the screen, the movie clip move till he reach that destination, it's like we are moving with arrow key and then we stop, i don't want that the movieclip start with a certain speed then end witt another value,

                 

                "how long should it take for the movieclip to reach the mouse position?"

                well that depend on the destination point and the speed value

                 

                thank you

                • 5. Re: Follow an object (mouse click) without decreasing the speed
                  prodigalmaster Level 2

                  Well this is not the easiest or sensible way but (there is a way this works with a few lines of code using the _rotation property, but here it is done without)...

                  on these lines of code:

                            this._x -= (this._x - this.goX)/20 ;

                            this._y -= (this._y - this.goY)/20 ;

                  I see you find the x and y distances and then divide that distances each by 20 and then make 'this' move by the numbers found.

                   

                  You want to find the ratio between the x and y distances. This is usually done by dividing the y by x. I think in mathematics they call it the gradient (m).

                   

                  So for example, if xDist is 200 and yDist is 150 then the ratio would be 150/200 which equals 0.75 - if the distances were swapped it would be 200/150 which is 1.3 - you want to discard any number above 1 (so it may be a better idea to divide xDist by yDist if yDist/xDist is > 1). Otherwise, it is possible to reach infinity or a very large number if for example xDist = 200 and yDist = 0.01

                   

                  Yeah, I'm fairly sure you have to create two gradients yDist/xDist and xDist/yDist and use whichever is 1 or less for the correct xSpeed or ySpeed (they relate in some way).

                   

                  OK, I hope you are still following.

                   

                  Now you have this low gradient number.. you want to multiply this number with one of the distances (x or y) and then make the other distance equal to 1... (not perfect, maybe you can think of a better way).

                   

                  If the speed is too fast then multiply both final speeds by some number below 1 (e.g. 0.5 is %50 of the original number).

                  • 6. Re: Follow an object (mouse click) without decreasing the speed
                    kglad Adobe Community Professional & MVP

                    if mc is the movieclip you want to move, you can use:

                     

                    import mx.transitions.Tween;

                    var speed:Number = 500;

                    var d:Number;

                    var t1:Tween;

                    var t2:Tween;

                     

                    function f():Void{

                        d = Math.sqrt((this._xmouse-mc._x)*(this._xmouse-mc._x)+(this._ymouse-mc._y)*(this._ymouse-mc ._y));

                        t1 = new Tween(mc, "_x", mx.transitions.easing.None.easeNone,mc._x,this._xmouse, d/speed, true);

                        t2 = new Tween(mc, "_y", mx.transitions.easing.None.easeNone,mc._y,this._ymouse, d/speed, true);

                    }

                     

                    this.onMouseMove = f;

                    • 7. Re: Follow an object (mouse click) without decreasing the speed
                      alaStickman Level 1

                      i can't understand how this gonna make the movie clip stop when he reach the wanted point,

                      thank you

                      • 8. Re: Follow an object (mouse click) without decreasing the speed
                        alaStickman Level 1

                        just another information maybe it's gonna help you to help me

                        what i want to do exactly is "car movement" with mouse click  it include rotation in the correct direction (which i also have a problem with it when i need the car to go to the opposite direction )

                        i hope this will give you an idea to help me found a solution

                        thank yoy 

                        • 9. Re: Follow an object (mouse click) without decreasing the speed
                          prodigalmaster Level 2

                          var moveSpeed = 6;

                          mc.onEnterFrame = function() {

                                    //calculate distances

                                    xDistance = _root._xmouse-this._x;

                                    yDistance = _root._ymouse-this._y;

                           

                                    // calculate the angle

                                    mcRadians = Math.atan2(yDistance, xDistance);

                           

                                    // convert to degrees and set rotation

                                    mcDegrees = 360*mcRadians/(2*Math.PI);

                                    this._rotation = mcDegrees;

                           

                                    //move according to rotation

                                    this._x += Math.cos(this._rotation*Math.PI/180)*moveSpeed;

                                    this._y += Math.sin(this._rotation*Math.PI/180)*moveSpeed;

                          };

                           

                          Where the identifier name is mc and the code is on the main timeline.

                          1 person found this helpful
                          • 10. Re: Follow an object (mouse click) without decreasing the speed
                            alaStickman Level 1

                            thank you, this is solved the speed issue, now i had one last problem, the rotation,

                            1st let me know if i had to close this discussion and start another one cause i found what i was looking for .

                            but maybe i can ask about it here,

                            so here is the rotation issue,

                            first here is my rotation code

                             

                             

                             

                            function rotateTowards(clip, targetX, targetY)

                            {

                                      var clipPoint = {x:clip._x, y:clip._y};

                                      clip._parent.localToGlobal(clipPoint);

                                      var deltaX = targetX - clipPoint.x;

                                      var deltaY = targetY - clipPoint.y;

                                      var rotationRadian = Math.atan2(deltaY, deltaX);

                                      var rotationAngle = radiansToDegrees(rotationRadian);

                                      clip._rotation -= (clip._rotation-rotationAngle)/8;

                             

                             

                            }

                            function radiansToDegrees(radians)

                            {

                                      return (radians / Math.PI) * 180;

                            }

                             

                            and i have this code on my car movieClip

                            onClipEvent (load) {

                                      rotateSpeed = 3;

                            }

                            onClipEvent (enterFrame) {

                             

                             

                                      onMouseUp = function ()

                                      {

                                                apui = false;

                                      };

                                      onMouseDown = function ()

                                      {

                                                apui = true;

                                                rotateSpeed = 3;

                                      };

                                      if (apui == true)

                                      {

                                                goX = _root._xmouse;

                                                goY = _root._ymouse;

                                                rotX = _root._xmouse;

                                                rotY = _root._ymouse;

                                                _root.rotateTowards(this,rotX,rotY);

                             

                             

                                      }

                                      else if (apui == false)

                                      {

                                                goX = this._x;

                                                goY = this._y;

                             

                             

                                      }

                             

                             

                                      _root.destination_mc._x = goX;

                                      _root.destination_mc._y = goY;

                                      this._x -= (this._x - this.goX) / 15;

                                      this._y -= (this._y - this.goY) / 15;

                             

                            }

                             

                            my problem is to make the rotation more "realistic" for example if we choose to go to the opposit direction i need the car to rollback then make a 1/2 turn then go tward the point

                            thank you