as3一些常用数学公式

基本三角函数的计算:

  • 角的正弦值 = 对边 / 斜边
  • 角的余弦值 = 邻边 / 斜边
  • 角的正切值 = 对边 / 邻边

角度制与弧度制的相互转换:

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

计算两点间距离:

dx = x2 – x1;
dy = y2 – y1;
dist = Math.sqrt(dx*dx + dy*dy);

缓动公式:

sprite.x += (targetX - sprite.x) * easing;//easing为缓动系数变量
sprite.y += (targetY - sprite.y) * easing;

弹性公式:

vx += (targetX - sprite.x) * spring;//spring为弹性系数
vy += (targetY - sprite.y) * spring;
sprite.x += (vx *= friction);//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;

向鼠标旋转(或向某点旋转)

dx = mouseX - sprite.x;
dy = mouseY - sprite.y;
sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;

波形运动:

public function onEnterFrame1(event:Event):void {
ball.y=centerScale+Math.sin(angle)*range
angle+=speed
}

心跳:

public function onEnterFrame1(event:Event):void {
ball.scaleX=centerScale+Math.sin(angle)*range;
ball.scaleY=centerScale+Math.sin(angle)*range;
angle+=speed;
}

圆心旋转:

public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radius;
ball.y=centerY+Math.sin(angle)*radius;
angle+=speed;
}

椭圆旋转:

public function onEnterFrame(event:Event):void {
ball.x=centerX+Math.cos(angle)*radiusX;
ball.y=centerY+Math.sin(angle)*radiusY;
angle+=speed;
}

颜色运算得到透明值:

var t:uint=0×77ff8877
var s:uint=0xff000000
var h:uint=t&s
var m:uint=h>>>24
trace(m)

转换进制:

  • 转换为十进制
    trace(hexValue);
  • 十进制转换为十六进制:
    decimalValue.toString(16)

颜色提取:

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

按位计算得到颜色值:

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

过控制点的曲线:

// xt, yt是你想要让曲线通过的那一点
// x0, y0 和x2, y2 是曲线的终点
//PS.发现很多人转帖都是直接复制粘贴,也不翻译一下
xt * 2 – (x0 + x2) / 2;
y1 = yt * 2 – (y0 + y2) / 2;
moveTo(x0, y0);
curveTo(x1, y1, x2, y2);

匀速移动到目标点

public function onEnterFrame(event:Event):void {
var X = targetX - ball.x;
var Y = targetY - ball.y;
var Z = Math.sqrt(X * Y + Y * Y); //求出距离
var t = ball.Speed / Z; //求出时间ball.Speed 为求的运动速度
var Vx = X / t; //横向速度
var Vy = Y / t; //纵向速度
ball.x += Vx;
ball.y += Vy;
if(Z <= ball.Speed){
ball.x = targetX;
ball.y = targetY;
return;
}//判断距离
}

移动到目标点 抖动特效 公式

functiononEnterFrame () :void{
v = v + easing * (targetX - ball._x);//easing为缓动系数变量
ball._x = ball._x + v;
v = spring * v;//spring为抖动幅度变量
}