A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: bouncing in place...

  1. #1
    pablo cruisin' hanratty21's Avatar
    Join Date
    Mar 2002
    Location
    on the lam
    Posts
    2,275

    bouncing in place...

    Ok - I can probably figure this one out on my own, but would love to get a quick answer on it instead....

    Basically, what I want is for something to bounce in place (like someone dribbling a basketball) when it is moused over. This needs to be scripted so the motion is smoooooooove.

    I hope this is enough info!

    RH
    "Why does it hurt when I pee?" -- F. Zappa |

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's a script that assumes there's a movie named 'mc' that you want to bounce 100 pixels. I'm using a sine-wave to achieve the bouncing effect. This looks nice & smooth if you use a fast frame rate, like 30 fps. It looks too jerky at 12 fps.

    You can achieve a different rate of bouncing by changing the .1 in the Math.sin equation to a different number. .05 will bounce twice as slow.

    Additional notes below.

    code:

    floorY = mc._y;
    bounceHeight = 100;

    mc.onEnterFrame = function()
    {
    if (this.isBouncing) {
    r = Math.abs(Math.sin((getTimer()-this.startBounce)*.1))*bounceHeight;
    this._y = floorY - r;
    if (r < 5 && getTimer() - this.startBounce > 1000) {
    this._y = floorY;
    this.isBouncing = false;
    }
    }
    }

    mc.onRollOver = function()
    {
    if (!this.isBouncing) {
    this.startBounce = getTimer();
    this.isBouncing = true;
    }
    }



    The problem with this, and any script that moves a movieclip in response to a rollover, is that when the movie moves, it will generally move out from under the mouse, causing a 'rollout' event right away. This means that if you attempt to stop bouncing when the mouse rolls out, the bouncing will stop right away when the ball moves out from under the mouse.

    There are a few ways to fix this. The method I chose here is to use the timer to control the bounce, and stop a fixed period of time (1000 milliseconds or 1 second) after the last rollIn event.

    Another method you can use is to use a separate 'frame' movieclip to respond to the rollovers that encompasses the entire bounce area, and not just the ball.

  3. #3
    pablo cruisin' hanratty21's Avatar
    Join Date
    Mar 2002
    Location
    on the lam
    Posts
    2,275
    jbum - Thanks for the quick response. I am going to try this out right now. Also, thanks for the tip on the mouseover stuff...actually, what I will be "bouncing" is the title of a button (just text) when the button itself is moused over...I will adjust the script accordingly.

    RH
    "Why does it hurt when I pee?" -- F. Zappa |

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