Flash - Ease out scrolling moviclip, scrolled by sensor on each side
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 nice 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?
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(project开发者_C百科ed_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
}
}
}
}
}
The link you provided doesn't work but I think I have a tip
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;
}
Basically you need to move the movieclip even when the mouse is not over the sensors.You accomplish this using the velocity variable.
You may need to tweak the values and add more code, this is just some concept code. Hope it helps you :)
精彩评论