A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: math formula

  1. #1
    Senior Member
    Join Date
    Mar 2001
    Posts
    197
    I got this code from a friend that makes objects move away from your mouse like a backwards magnet, then moves back into place after the mouse is gone. I want to upgrade the code to scale the object down the further away it moves out of place, then return to full size when it moves back into place. I've tried a few diffrent things, nothing worked, I am pritty much confused how to do it at this point. If anybody could help me I would really appreciate it. Thanks in advance.

    code------------------
    //variables loaded once
    onClipEvent(load){
    homeX = this._x
    homeY = this._y
    magnet = 500
    }

    //variables = sub formula that update every new frame
    onClipEvent(enterFrame){
    x0 = this._x
    y0 = this._y
    x1 = _root._xmouse
    y1 = _root._ymouse
    distancex = x1-x0
    distancey = y1-y0
    distance = Math.sqrt((distancex * distancex) + (distancey * distancey))
    powerx = this._x - (distancex / distance) * magnet / distance
    powery = this._y - (distancey / distance) * magnet / distance
    forcex = (forcex + (homex - x0) / 2) / 1.66
    forcey = (forcey + (homey - y0) / 2) / 1.66

    //formula
    this._x = powerx + forcex
    this._y = powery + forcey
    }
    ------------------------

    This is a link to see what the code is doing now.
    http://www.geocities.com/beatlemier

    Thanks again for any help.

  2. #2
    try this:

    add this to the onload event

    onload:

    myheight=this._height;
    mywidth=this._width;





    at the end of the script add:

    sizex=this._x-homex;
    sizey=this._y-homey;

    size=1-(0.5*(Math.sqrt((sizex*sizex)+(sizey*sizey))/anyvariable));
    this._height=myheight*size;
    this._width=mywidth*size;


    anyvariable is a number that will be more than the heighest distance possible from the mouse. eg. if your movie is 500x400, choose something like 500. Anyway, play around with the number and choose the best one for you.


    this way the smallest size it can get to is 0.5 of its original. You can make it smaller by making the 0.5 0.7 etc...


    hope that helps.

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