Make Things Move


基礎三角函數的計算:

角的正弦值 = 對邊 / 斜邊
角的余弦值 = 鄰邊 / 斜邊
角的正切值 = 對邊 / 鄰邊

弧度轉換為角度以及角度轉換為弧度:

弧度 = 角度 * Math.PI / 180
角度 = 弧度 * 180 / Math.PI

向鼠標(或者任何一個點)旋轉:

// 用要旋轉到的 x, y 坐標替換 mouseX, mouseY
dx = mouseX - sprite.x;
dy = mouseY - sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

創建波形:

// 將 x, y 或其它屬性賦值給 Sprite 影片或影片剪輯,
public function onEnterFrame(event:Event){
     value = center + Math.sin(angle) * range;
     angle += speed;
}

創建圓形:

// 將 x, y 或其它屬性賦值給 Sprite 影片或影片剪輯,
// 作為繪圖坐標,等等。
public function onEnterFrame(event:Event){
     xposition = centerX + Math.cos(angle) * radius;
     yposition = centerY + Math.sin(angle) * radius;
     angle += speed;
}

創建橢圓:

// 將 x, y 或其它屬性賦值給 Sprite 影片或影片剪輯,
// 作為繪圖坐標,等等。
public function onEnterFrame(event:Event){
     xposition = centerX + Math.cos(angle) * radiusX;
     yposition = centerY + Math.sin(angle) * radiusY;
     angle += speed;
}

獲得兩點間的距離:

// x1, y1 和 x2, y2 是兩個點
// 也可以是 Sprite / MovieClip 坐標,鼠標坐標,等等。
dx = x2 – x1;
dy = y2 – y1;
dist = Math.sqrt(dx*dx + dy*dy);

十六進制轉換為十進制:

trace(hexValue);

十進制轉換為十六進制:

trace(decimalValue.toString(16));

顏色組合:

color24 = red << 16 | green << 8 | blue;
color32 = alpha << 24 | red << 16 | green << 8 | blue;

顏色提取:

red = color24 >> 16;
green = color24 >> 8 & 0xFF;
blue = color24 & 0xFF;
alpha = color32 >> 24;
red = color32 >> 16 & 0xFF;
green = color32 >> 8 & 0xFF;
blue = color232 & 0xFF;

穿過某點繪制曲線:

// xt, yt 是我們想要穿過的一點
// x0, y0 以及 x2, y2 是曲線的兩端
x1 = xt * 2 – (x0 + x2) / 2;
y1 = yt * 2 – (y0 + y2) / 2;
moveTo(x0, y0);
curveTo(x1, y1, x2, y2);

角速度轉換為 x, y 速度:

vx = speed * Math.cos(angle);
vy = speed * Math.sin(angle);

角加速度(作用於物體上的 force)轉換為 x, y 加速度:

ax = force * Math.cos(angle);
ay = force * Math.sin(angle);

將加速度加入速度:

vx += ax;
vy += ay;

將速度加入坐標:

movieclip._x += vx;
sprite.y += vy;

移除出界對象:

if (sprite.x - sprite.width / 2 > right ||
sprite.x + sprite.width / 2 < left ||
sprite.y – sprite.height / 2 > bottom ||
sprite.y + sprite.height / 2 < top)
{
// 刪除影片的代碼

}

重置出界對象:

if(sprite.x - sprite.width / 2 > right ||
     sprite.x + sprite.width / 2 < left ||
     sprite.y – sprite.height / 2 > bottom ||
     sprite.y + sprite.height / 2 < top)
{
// 重置影片的位置和速度
}

屏幕環繞出界對象:

if(sprite.x - sprite.width / 2 > right)
{
sprite.x = left - sprite.width / 2;
}
else if(sprite.x + sprite.width / 2 < left)
{
sprite.x = right + sprite.width / 2;
}
if(sprite.y – sprite.height / 2 > bottom)
{
sprite.y = top – sprite.height / 2;
}
else if(sprite.y + sprite.height / 2 < top)
{
sprite.y = bottom + sprite.height / 2;
}

摩擦力應用(正確方法):

speed = Math.sqrt(vx * vx + vy * vy);
angle = Math.atan2(vy, vx);
if(speed > friction)
{
speed -= friction;
}
else
{
speed = 0;
}
vx = Math.cos(angle) * speed;
vy = Math.sin(angle) * speed;

摩擦力應用(簡便方法):

vx *= friction;
vy *= friction;

簡單緩動運動,長形:

var dx:Number = targetX - sprite.x;
var dy:Number = targetY - sprite.y;
vx = dx * easing;
vy = dy * easing;
sprite.x += vx;
sprite.y += vy;

簡單緩動運動,中形:

vx = (targetX - sprite.x) * easing;
vy = (targetY - sprite.y) * easing;
sprite.x += vx;
sprite.y += vy;

簡單緩動運動,短形:

sprite.x += (targetX - sprite.x) * easing;
sprite.y += (targetY - sprite.y) * easing;

簡單彈性運動,長形:

var ax:Number = (targetX - sprite.x) * spring;
var ay:Number = (targetY - sprite.y) * spring;
vx += ax;
vy += ay;
vx *= friction;
vy *= friction;
sprite.x += vx;
sprite.y += vy;

簡單彈性運動,中形:

vx += (targetX - sprite.x) * spring;
vy += (targetY - sprite.y) * spring;
vx *= friction;
vy *= friction;
sprite.x += vx;
sprite.y += vy;

簡單彈性運動,短形:

vx += (targetX - sprite.x) * spring;
vy += (targetY - sprite.y) * spring;
sprite.x += (vx *= friction);
sprite.y += (vy *= friction);

偏移彈性運動:

var dx:Number = sprite.x - fixedX;
var dy:Number = sprite.y - fixedY;
var angle:Number = Math.atan2(dy, dx);
var targetX:Number = fixedX + Math.cos(angle) * springLength;
var targetY:Number = fixedX + Math.sin(angle) * springLength;
// 如前例彈性運動到 targetX, targetY

距離碰撞檢測:

// 從影片 spriteA 和 spriteB 開始
// 如果使用一個空白影片,或影片沒有半徑(radius)屬性
// 可以用寬度或高度除以 2。
var dx:Number = spriteB.x - spriteA.x;
var dy:Number = spriteB.y - spriteA.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
if(dist < spriteA.radius + spriteB.radius)
{
// 處理碰撞
}

多物體碰撞檢測:

var numObjects:uint = 10;
for(var i:uint = 0; i < numObjects - 1; i++)
{
// 使用變量 i 提取引用
var objectA = objects[i];
for(var j:uint = i+1; j
{
// // 使用變量 j 提取引用
var objectB = objects[j];
// perform collision detection
// between objectA and objectB
}
}

坐標旋轉:

x1 = Math.cos(angle) * x - Math.sin(angle) * y;
y1 = Math.cos(angle) * y + Math.sin(angle) * x;

反坐標旋轉:

x1 = Math.cos(angle) * x + Math.sin(angle) * ;y
y1 = Math.cos(angle) * y - Math.sin(angle) * x;

動量守恆的數學表達式:

          (m0 – m1) * v0 + 2 * m1 * v1
v0Final = ----------------------------------------------
          m0 + m1
          (m1 – m0) * v1 + 2 * m0 * v0
v1Final = ---------------------------------------------
          m0 + m1

動量守恆的 ActionScript 表達式,短形:

var vxTotal:Number = vx0 - vx1;
vx0 = ((ball0.mass - ball1.mass) * vx0 +
2 * ball1.mass * vx1) /
(ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;

引力的一般公式:

force = G * m1 * m2 / distance2

ActionScript 實現萬有引力:

function gravitate(partA:Ball, partB:Ball):void
{
var dx:Number = partB.x - partA.x;
var dy:Number = partB.y - partA.y;
var distSQ:Number = dx * dx + dy * dy;
var dist:Number = Math.sqrt(distSQ);
var force:Number = partA.mass * partB.mass / distSQ;
var ax:Number = force * dx / dist;
var ay:Number = force * dy / dist;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}

餘弦定理

a2 = b2 + c2 – 2 * b * c * cos A
b2 = a2 + c2 – 2 * a * c * cos B
c2 = a2 + b2 – 2 * a * b * cos C

ActionScript 的余弦定理:

A = Math.acos((b * b + c * c - a * a) / (2 * b * c));
B = Math.acos((a * a + c * c - b * b) / (2 * a * c));
C = Math.acos((a * a + b * b - c * c) / (2 * a * b));

基本透視法:

scale = fl / (fl + zpos);
sprite.scaleX = sprite.scaleY = scale;
sprite.alpha = scale; // 可選
sprite.x = vanishingPointX + xpos * scale;
sprite.y = vanishingPointY + ypos * scale;

Z 排序:

// 假設有一個帶有 zpos 屬性的 3D 物體的數組
objectArray.sortOn("zpos", Array.DESCENDING | Array.NUMERIC);
for(var i:uint = 0; i < numObjects; i++)
{
setChildIndex(objectArray[i], i);
}

坐標旋轉:

x1 = cos(angleZ) * xpos – sin(angleZ) * ypos;
y1 = cos(angleZ) * ypos + sin(angleZ) * xpos;
x1 = cos(angleY) * xpos – sin(angleY) * zpos;
z1 = cos(angleY) * zpos + sin(angleY) * xpos;
y1 = cos(angleX) * ypos – sin(angleX) * zpos;
z1 = cos(angleX) * zpos + sin(angleX) * ypos;

3D 距離:

dist = Math.sqrt(dx * dx + dy * dy + dz * dz);