Skip to main content
Inspiring
October 13, 2012
Question

Follow an object (mouse click) without decreasing the speed

  • October 13, 2012
  • 1 reply
  • 2160 views

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 ;

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
October 13, 2012

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

Inspiring
October 13, 2012

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

thank you

Inspiring
October 13, 2012

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


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).