第三部分 高级运动
第八章 缓动和弹性
- 缓动:越是靠近目标越慢
var vx:Number = (targetX - ball.x) * easing;
var vy:Number = (targetY - ball.y) * easing;
ball.x += vx;
ball.y += vy;
何时停止缓动?
If(Math.abs(dx)<1){
Ball.x = target;
removeEventListener(Event.ENTER_FRAME);
trace(“done”);
}
跟随目标移动:
var vx:Number = (mouseX - ball.x) * easing;
var vy:Number = (mouseY - ball.y) * easing;
ball.x += vx;
ball.y += vy;
- 缓动不只是应用于运动:
透明度:ball.alpha += (targetAlpha-ball.alpha)*easing;
旋转:arrow.rotation += (targetRotation-arrow.rotation)*easing;
颜色:red += (redTarget - red)*easing;
- 弹性:(一维弹性)越是靠近目标越快(初始位置和目标位置保持一定距离,以便加速)
var dx:Number = targetX - ball.x;
var ax:Number = dx * spring;
vx += ax;
ball.x += vx;
加入摩擦值:
var dx:Number = targetX - ball.x;
var ax:Number = dx * spring;
vx += ax;
vx *= friction;//0.95
ball.x += vx;
二维弹性:带摩擦力
var dx:Number = targetX - ball.x;
var dy:Number = targetY - ball.y;
var ax:Number = dx * spring;
var ay:Number = dy * spring;
vx += ax;
vy += ay;
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;
移动目标点的弹性:跟随鼠标弹动(将targetX更改为mouseX就OK了)
var dx:Number = mouseX - ball.x;
var dy:Number = mouseY - ball.y;
画出求与鼠标间的线:
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(mouseX, mouseY);
graphics.lineTo(ball.x, ball.y);
多目标点弹性:
利用函数调用、循环、数组实现