Flash - Ease out scrolling moviclip, scrolled by sensor on each side

Posted by webfac on Stack Overflow See other posts from Stack Overflow or by webfac
Published on 2010-04-09T07:50:32Z Indexed on 2010/04/09 7:53 UTC
Read the original article Hit count: 350

I am hoping someone can shed some light on this issue for me... I have a movieclip that is scrolled by means of a 'sensor' on each side of the stage. The clip scrolls fine in both directions, however here is my problem:

When the users mouse leaves the stage, the movie clip stops dead in it's tracks, and this does not provide a noce smooth effect. Looking at the code below, is there any way I could tell the animation to ease out when the users mouse leaves the stage rather than simply stop suddenly? Much appreciated!

class Sensor extends MovieClip {

    public function Sensor() {
    }

    public function onEnterFrame() {

        //

        var active:Boolean;

        // is mouse within sensor?
        if ((_level2._xmouse >= this._x) && _level2._xmouse <= (this._x + this._width)) {
            active = true;      
        } else {
            active = false;
        }

        if(active) {

            // which area of the sensor is it in?
            var relative_position:Number;
            var relative_difference:Number;
            relative_position = _level2._xmouse / this._width * 100.0;

            //_level2._logger.message("relative position is " + relative_position);

            if (!isNaN(relative_position)) {

                // depending on which area it is in, tend towards a particular adjustment
                if(relative_position > _level2._background_right_threshold) {
                    relative_difference = Math.abs(relative_position - _level2._background_right_threshold);
                    //relative_difference = 10;
                } else if(relative_position < _level2._background_left_threshold) { 
                    relative_difference = Math.abs(_level2._background_left_threshold - relative_position);
                    relative_difference *= -1;
                } else {
                    _level2._background_ideal_adjustment = 0;
                }


                var direction:Number;
                if(_level2._pan_direction == "left") {
                    direction = 1;
                } else {
                    direction = -1;
                }
                _level2._background_ideal_adjustment = direction * (relative_difference / 10) * _level2._pan_augmentation;
                //_level2._logger.message("ideal adjustment is " + _level2._background_ideal_adjustment);
                if (!isNaN(_level2._background_ideal_adjustment)) {

                    // what is the difference between the ideal adjustment and the current adjustment?
                    // add a portion of the difference to the adjustment:
                    // this has the effect that the adjustment "tends towards" the ideal
                    var difference:Number;
                    difference = _level2._background_ideal_adjustment - _level2._background_adjustment;
                    _level2._background_adjustment += (difference / _level2._background_tension);

                    // calculate what the new background _x position would be
                    var background_x:Number;
                    var projected_x:Number;
                    background_x = _level2["_background"]._x;
                    projected_x = background_x += _level2._background_adjustment;
                    //_level2._logger.message("projected _x is " + projected_x);

                    // if the _x position is valid, change it
                    if(projected_x > 0) projected_x = 0;
                    if(projected_x < -(_level2["_background"]._width - Stage.width)) projected_x = -(_level2["_background"]._width - Stage.width);
                    _level2["_background"]._x = projected_x;

                    // i recommend that you move your other movieclips with code here

                }

            }

        }

    }

}

© Stack Overflow or respective owner

Related posts about flash

Related posts about actionscript