dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: HOW Make a MC follow mouse ???

  1. #1
    Member
    Join Date
    Jan 2003
    Location
    belgium
    Posts
    52

    HOW Make a MC follow mouse ???

    Hi, how can you make a movieclip follow your mouse with a little bit delay, like an elastic rubber ...




    please help...
    hi, how are you?

  2. #2
    Senior Member zeroreality's Avatar
    Join Date
    Jun 2000
    Posts
    130
    lol, i just finished writing this code
    Code:
    onClipEvent (enterFrame) {
    	thisX = this._x
    	thatX = _root.target._x
    	distX = (distX/1.1) + ((thatX - thisX)/5)
    	moveX = thisX + distX
    	this._x = moveX 
    }
    ok, now an explanation

    all this was written with moving along the X axis alone... if you want to do Y axis as well... then just do the same thing twice and replace all the Xs with Ys

    thisX is obviously the object that's moving
    thatX is the target... can be another object or your mouse
    distX is the distance the object will travel
    moveX is the X co-ordinate it will be at on the next 'enterFrame'
    1.1 is just some value i made up... but that is basically the 'elasticity' of the object
    5 is another value i made up... its the speed of the object


    distX is where the magic happens.
    distX is added to itself again if you notice distX = (distX/1.1).......
    this is done to calculate how fast the object moved the PREVIOUS time thru the loop. the 1.1 value simply makes that value a bit smaller (slowing it down)

    so what you get is a series of calculations of where to travel that is SLIGHTLY longer than what it needs to be. this means it trys to overshoot the target every time thru the loop.

    keep the 1.1 as low as possible.. if the value is set to 1 then it never stops and constantly overshoots the target. a value below 1 like 0.9 makes the object go away from the target.

    the 5 is just some speed, the higher it is, the slower the object travels... if its traveling too slow, the 1.1 doesnt affect it and you'd need to adjust that value too

    hope this isnt TOO complicated :P

  3. #3
    Member
    Join Date
    Jan 2003
    Location
    belgium
    Posts
    52
    ok it works, thanx very much
    hi, how are you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center