0

我正在尝试制作弹性跟随鼠标。一切正常。但唯一的问题是:我不希望它反弹很长时间。我想设置持续时间以使反弹仅 1 秒。这个怎么做 ?这是我使用的 As3 脚本:

var distx_L     = 0;
var disty_L     = 0;
var momentumx_L = 0;
var momentumy_L = 0;
var easing_L = 5;


stage.addEventListener(Event.ENTER_FRAME,elastic_follow)
function elastic_follow(event:Event):void {


    distx_L = myball.x - mouseX;
    disty_L = myball.y - mouseY;
    momentumx_L -= distx_L / easing_L;  
    momentumy_L -= disty_L /easing_L; 

    // dampen the momentum a little (ELASTIC)
    momentumx_L*= 0.90;
    momentumy_L *= 0.90;


    myball.x += momentumx_L-15;
    myball.y += momentumy_L;

}
4

1 回答 1

0

这应该是调整你的价值观的问题。我建议提取 0.90 作为变量,以便您可以更轻松地调整它:

var distx_L:Number     = 0;
var disty_L:Number     = 0;
var momentumx_L:Number = 0;
var momentumy_L:Number = 0;
var easing_L:Number = 1;
var ease:Number = 0.5;

stage.addEventListener(Event.ENTER_FRAME,elastic_follow)
function elastic_follow(event:Event):void {


    distx_L = myball.x - mouseX;
    disty_L = myball.y - mouseY;
    momentumx_L -= distx_L / easing_L;  
    momentumy_L -= disty_L /easing_L; 

    // dampen the momentum a little (ELASTIC)
    momentumx_L*= ease;
    momentumy_L *= ease;


    myball.x += momentumx_L;
    myball.y += momentumy_L;

}

如果您想在多个剪辑上使用这种行为,您可能希望为每个剪辑设置弹性和缓动变量。这是一个懒惰的方法:

myball.mx = myball2.mx = 0;//mx for momentum on x axis
myball.my = myball2.my = 0;//my for momentum on x axis
myball.k = 4;//k for elasticity
myball.ease = .75;//ease for the final ease
myball2.k = 6;
myball2.ease = .8;

stage.addEventListener(Event.ENTER_FRAME,elastic_follow)
function elastic_follow(event:Event):void {
    elasticToPos(myball,mouseX-myball.width,mouseY);
    elasticToPos(myball2,mouseX+myball.width,mouseY);
    //elasticToPos(myball2,myball.x+myball.width*2,myball.y);//you can also play with this - have the second clip chase the first rather than the mouse
}

function elasticToPos(clip:MovieClip,targetX:Number,targetY:Number):void{
    var dx:Number = clip.x - targetX;
    var dy:Number = clip.y - targetY;
    clip.mx -= dx / clip.k;
    clip.my -= dy / clip.k;
    clip.mx *= clip.ease;
    clip.my *= clip.ease;
    clip.x += clip.mx;
    clip.y += clip.my;
}

请注意,我为两个剪辑设置了不同的弹性 (k) 和缓动值,因此它们的行为略有不同。正如我之前提到的,使用这些值会得到你想要的效果。

附带说明一下,动态地将变量添加到动态 MovieClip 类并不是最佳实践。您应该将 Sprite 子类化,并向其中添加轻松/弹性变量和更新方法。

于 2013-06-29T20:56:06.017 回答