摘自Robert Penner的Programming Marcomedia Flash MX
(提示:以下代码中的Vector是一个AS1.0的自定义类,这个Vector类的代码请到作者的网站下载)
一、运动学
位置
// 用二维向量表示的位置
pos = new Vector(2, 5);位移
// 计算位移向量
pointA = new Vector(2, 3);
pointB = new Vector(4, 2);
disp = pointB.minusNew(pointA);
trace(disp); // 输出:[2,-1]距离
// 继续前面的例子
trace(disp.getLength()); // 输出:2.23606797749979速度
- 恒定速度的运动
// 恒定速度的运动
pos = new Vector(0, 0);
vel = new Vector(2, 3);
this.onEnterFrame = function() {
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
}; - 随机速度的运动
// 随机速度的运动
pos = new Vector(200, 200);
vel = new Vector(0, 0);
this.onEnterFrame = function() {
vel.reset(random(9)-4, random(9)-4);
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
}; - 以鼠标控制速度的运动
// 以鼠标控制速度
pos = new Vector(200, 200);
vel = new Vector(0, 0);
this.onEnterFrame = function() {
vel.reset(this._xmouse/10, this._ymouse/10);
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
};
- 恒定速度的运动
速率
vel = new Vector(3,4);
speed = vel.getLength();
trace(speed); // 输出:5加速度
- 恒定加速度的运动
// 恒定加速度的运动
pos = new Vector(0, 0);
vel = new Vector(0, 0);
accel = new Vector(1, 0);
this.onEnterFrame = function() {
vel.plus(accel);
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
}; - 随机加速度的运动
// 随机加速度的运动
pos = new Vector(200, 200);
vel = new Vector(0, 0);
accel = new Vector(0, 0);
this.onEnterFrame = function() {
accel.reset(random(5)-2, random(5)-2);
vel.plus(accel);
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
}; - 以鼠标控制加速度的运动
// 以鼠标控制加速度
pos = new Vector(200, 200);
vel = new Vector(0, 0);
accel = new Vector(0, 0);
this.onEnterFrame = function() {
accel.reset(this._xmouse/100, this._ymouse/100);
vel.plus(accel);
pos.plus(vel);
this._x = pos.x;
this._y = pos.y;
};
- 恒定加速度的运动
内文分页: [1] [2]
关于Web设计的笔记

