代码之家  ›  专栏  ›  技术社区  ›  SimonDowdles

flash-通过两侧的传感器滚动,使滚动动作变得轻松

  •  0
  • SimonDowdles  · 技术社区  · 16 年前

    我希望有人能为我澄清这个问题…我有一个电影剪辑是通过一个'传感器'在舞台的每一边滚动。剪辑在两个方向上滚动都很好,但这里是我的问题:

    当用户鼠标离开舞台时,电影剪辑在其轨迹中停止,这并不能提供良好的平滑效果。看看下面的代码,有没有什么方法可以让动画在用户鼠标离开舞台时放松,而不是突然停止?

    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
    
                    }
    
                }
    
            }
    
        }
    
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Oliver    15 年前

    你提供的链接不起作用,但我想我有个提示

    velocity=0;
    maxVelocity=3;
    accel=0.1;
    break=0.98;
    
    
    function onEnterFrame(){
        if (mouseOverRightSensor){
            velocity +=accel; 
        }else if (mouseOverLeftSensor){
         velocity -=accel;
        }
       // make sure you constrain the velocity
       if (Math.abs(velocity)>maxVelocity) {  
       //get the sign of the velocity and multiply it with maxVelocity
          velocity=Math.abs(velocity)/velocity * maxVelocity
       }
       //Add the velocity to the movieclip's position
       movieclip.x+=velocity;
       //shrink down the velocity, this is done each frame no matter if the mouse is over the sensor;
       velocity*=break;
    }
    

    基本上,即使鼠标不在传感器上,你也需要移动movieclip,这是用速度变量来实现的。

    您可能需要调整这些值并添加更多代码,这只是一些概念代码。 希望对你有帮助:)

    推荐文章