AS3动画效果公式,常用处理公式代码,基本运动公式,三角公式
as3种常见的弹性效果公式以及波形运动等as3动画效果公式代码整理,对于常用的来说作者整理的很全面,包括AS3的进制转换颜色提取等效果:
AS3缓动公式:
1 sprite.x += (targetX – sprite.x) * easing;//easing为缓动系数变量sprite.y += (targetY – sprite.y) * easing;
AS3弹性公式:
1 vx += (targetX – sprite.x) * spring;//spring为弹性系数vy += (targetY – sprite.y) * spring;sprite.x += (vx *= friction);//friction为摩擦力sprite.y += (vy *= friction);
AS3偏移弹性公式:
1 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;
AS3向鼠标旋转(或向某点旋转)
1 dx = mouseX – sprite.x;dy = mouseY – sprite.y;sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
AS3波形运动:
1 public function onEnterFrame1(event:Event):void {ball.y=centerScale+Math.sin(angle)*range;angle+=speed;}
心跳:
1 public function onEnterFrame1(event:Event):void {ball.scaleX=centerScale+Math.sin(angle)*range;ball.scaleY=centerScale+Math.sin(angle)*range;angle+=speed;}
AS3圆心旋转:
1 public function onEnterFrame(event:Event):void {ball.x=centerX+Math.cos(angle)*radius;ball.y=centerY+Math.sin(angle)*radius;angle+=speed;}
椭圆旋转:
1 public function onEnterFrame(event:Event):void {ball.x=centerX+Math.cos(angle)*radiusX;ball.y=centerY+Math.sin(angle)*radiusY;angle+=speed;}
AS3颜色运算得到透明值:
1 var t:uint=0×77ff8877var s:uint=0xff000000var h:uint=t&svar m:uint=h>>>24trace(m)
AS3转换为十进制:
trace(hexValue);
AS3十进制转换为十六进制:
1 decimalValue.toString(16)
AS3颜色提取:
1 red = color24 >> 16;green = color24 >> 8 & 0xFF;blue = color24 & 0xFF;alpha = color32 >> 24;red = color32 >> 16 & 0xFF;green = color32 >> 8 & 0xFF;blue = color232 & 0xFF;
AS3按位计算得到颜色值:
1 color24 = red < < 16 | green << 8 | blue;color32 = alpha << 24 | red << 16 | green << 8 | blue;
AS3过控制点的曲线:
1 // xt, yt is the point you want to draw through// x0, y0 and x2, y2 are the end points of the curvex1 = xt * 2 – (x0 + x2) / 2;y1 = yt * 2 – (y0 + y2) / 2;moveTo(x0, y0);curveTo(x1, y1, x2, y2);
AS3右键菜单:
建立右键菜单项:
1 var cm:ContextMenu=new ContextMenu();cm.hideBuiltInItems();//隐藏系统菜单(不全隐藏)cm.builtInItems指向右键菜单的系统项是个ContextMenuBuiltInItems对象如: cm.builtInItems.print=true;使其打印可选cm.customItems是个数字指定右键菜单项目new ContextMenuItem(“菜单1″,false,true,true)建立一个右键菜单项目,必须指定到customItem中,第一个参数为是否上面出现间隔线,第二个为是否禁用,第三个为是否显示;如: cm.customItems.push(new ContextMenuItem(“hello”,false,false,true))
常用事件:
1 cm.addEventListener(ContextMenuEvent.MENU_SELECT,function(){trace(“ss”)})//当右键菜单触发时cm.customItems[i].addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,MenuItemSelectHandler);//当某个选择项触发时e.currentTarget.caption;得到相应的caption
-----------------------------------------------------------
缓动和弹性
简单缓动:
1 // 长形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;
简单弹性:
1 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;sprite.x += (vx *= friction);sprite.y += (vy *= friction);
偏移弹性运动:
1 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 = fixedY + Math.sin(angle) * springLength;
碰撞检测
距离碰撞检测:
1 //从spriteA 和 spriteB开始,如果使用一个空白影片,或影片没有半径(radius)属性//可以用高度与宽度除以2var 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.randius + spriteB.radius) {//处理碰撞}
多物体碰撞检测:
1 var numObjects:uint = 10;for (var i :uint = 0; i < numObjects -1; i++) {var objectA = object[i ];for (var j :uint = 0; i < numObjects; j++) {var objectB = object[j ];// 在 objectA 与 objectB 之间进行碰撞检测}}
坐标旋转和角度回弹
坐标旋转:
1 x1 = Math.cos(angle) * x - Math.sin(angle) * y;y1 = Math.sin(angle) * y + Math.cos(angle) * x;
反坐标旋转:
1 x1 = Math.cos(angle) * y + Math.sin(angle) * y;y1 = Math.cos(angle) * y - Math.sin(angle) * y;
撞球物理
动量:p = m * v
动量守恒:(m0 * v0) + (m1 * v1) = (m0 * v0Final) + (m1 * v1Final)
动能:KE = 0.5 * m * v²
动能守恒:(0. 5 * m0 * v0²) + (0.5 * m1 * v1²) = (0.5 * m0 * v0Final²) + (0.5 * m1 * v1Final²)
动量守恒的数学表达式:
1 v0Final = ((m0 - m1) * v0 + 2 * m1 * v1 ) / (m0 + m1)v1Final = ((m1 - m0) * v1 + 2 * m0 * v0 ) / (m0 + m1)
动量守恒的ActionScript表达式:
1 var vxTotal:Number = vx0 - vx1;vx0 = ((ball0.mass - ball1.mass) * vx0 + 2 * ball1.mass * vx1 ) / (ball0.mass + ball1.mass);vx1 = vxTotal + vx0;
粒子(Particles)引力和重力
引力的一般公式:
1 force = G * m1 * m2 / distance²
2 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;}
------------------------------------------------------------
速度与加速度
角速度转换为x,y速度向量:
1 vx = speed * Math.cos(angle);vy = speed * Math.sin(angle);
角加速度(作用于物体上的force)转换为x,y加速度:
1 ax = force * Math.cos(angle);ay = force * Math.sin(angle);
将加速度加入速度向量:
1 vx += ax;vy += ay;
将速度向量加入坐标:
1 sprite.x += vx;sprite.y += vy;
边界与摩擦力
处理出界对象:
1 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 ){// 移除对象代码(或 重置对象代码)}
屏幕环绕出界对象:
1 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 = sprite.y + sprite.height / 2;}
摩擦力应用(正确方法):
1 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;
摩擦力应用(简便方法):
1 vx *= friction;vy *= friction;
———————————————————–
缓动公式:
1 sprite.x += (targetX – sprite.x) * easing;//easing为缓动系数变量
2 sprite.y += (targetY – sprite.y) * easing;
弹性公式:
1 vx += (targetX – sprite.x) * spring;//spring为弹性系数
2 vy += (targetY – sprite.y) * spring;
3 sprite.x += (vx *= friction);//friction为摩擦力
4 sprite.y += (vy *= friction); 偏移弹性公式:
5 var dx:Number = sprite.x – fixedX;
6 var dy:Number = sprite.y – fixedY;
7 var angle:Number = Math.atan2(dy, dx);
8 var targetX:Number = fixedX + Math.cos(angle) * springLength;
9 var targetY:Number = fixedX + Math.sin(angle) * springLength;
10 向鼠标旋转(或向某点旋转)
11 dx = mouseX – sprite.x; dy = mouseY – sprite.y;
12 sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
波形运动:
1 public function onEnterFrame1(event:Event):void {
2 ball.y=centerScale+Math.sin(angle)*range;
3 angle+=speed;
4 }
心跳:
1 public function onEnterFrame1(event:Event):void {
2 ball.scaleX=centerScale+Math.sin(angle)*range;
3 ball.scaleY=centerScale+Math.sin(angle)*range;
4 angle+=speed;
5 }
圆心旋转:
1 public function onEnterFrame(event:Event):void {
2 ball.x=centerX+Math.cos(angle)*radius;
3 ball.y=centerY+Math.sin(angle)*radius;
4 angle+=speed;
5 }
椭圆旋转:
1 public function onEnterFrame(event:Event):void {
2 ball.x=centerX+Math.cos(angle)*radiusX;
3 ball.y=centerY+Math.sin(angle)*radiusY;
4 angle+=speed;
5 }
按位计算得到颜色值:
1 color24 = red << 16 | green << 8 | blue;
2 color32 = alpha << 24 | red << 16 | green << 8 | blue;
过控制点的曲线:
1 // xt, yt is the point you want to draw through // x0, y0 and x2, y2 are the end points of the curve x1 = xt * 2 – (x0 + x2) / 2; y1 = yt * 2 – (y0 + y2) / 2; moveTo(x0, y0); curveTo(x1, y1, x2, y2);
2 向鼠标旋转(或向某点旋转)dx = mouseX – sprite.x;dy = mouseY – sprite.y;sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
波形运动:
1 public function onEnterFrame1(event:Event):void {ball.y=centerScale+Math.sin(angle)*range;angle+=speed;}
心跳:
1 public function onEnterFrame1(event:Event):void {ball.scaleX=centerScale+Math.sin(angle)*range;ball.scaleY=centerScale+Math.sin(angle)*range;angle+=speed;}
圆心旋转
1 public function onEnterFrame(event:Event):void {ball.x=centerX+Math.cos(angle)*radius;ball.y=centerY+Math.sin(angle)*radius;angle+=speed;}
椭圆旋转
1 public function onEnterFrame(event:Event):void {ball.x=centerX+Math.cos(angle)*radiusX;ball.y=centerY+Math.sin(angle)*radiusY;angle+=speed;}
勾股定理
1 两个点坐标分别为x1,y1和x2,y2,那么两点间距离为var dx:Number = sprite1.x – sprite2.x;var dy:Number = sprite1.y – sprite2.y;var dist:Number = Math.sqrt(dx * dx + dy * dy);注:Math.sqrt是开平方的意思
画布中随机坐标
1 sprite1.x = Math.random() * stage.stageWidth;sprite1.y = Math.random() * stage.stageHeight;
鼠标到Sprite之间连线
1 public function onMouseMove(event:MouseEvent):void {graphics.clear();graphics.lineStyle(1, 0, 1);graphics.moveTo(sprite1.x, sprite1.y);graphics.lineTo(mouseX, mouseY);var dx:Number=sprite1.x-mouseX;var dy:Number=sprite1.y-mouseY;var dist:Number=int(Math.sqrt(dx*dx+dy*dy));textField.text=dist.toString();}
基本三角函数的计算:
角的正弦值 = 对边 / 斜边
角的余弦值 = 邻边 / 斜边
角的正切值 = 对边 / 邻边
角度制与弧度制的相互转换:
弧度 = 角度 * Math.PI / 180
角度 = 弧度 * 180 / Math.PI
计算两点间距离
1 dx = x2 – x1;dy = y2 – y1;dist = Math.sqrt(dx*dx + dy*dy);
偏移弹性公式:
1 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;
过控制点的曲线:
1 // 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);
// 重置影片的位置和速度
1 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){ }
摩擦力应用(正确方法):
1 DE>speed = Math.sqrt(vx * vx + vy * vy); DE>DE>angle = Math.atan2(vy, vx); DE>DE>ifDE>DE>(speed > friction){ DE>DE>speed -= friction; DE>DE>}DE>DE>elseDE>DE>{ DE>DE>speed = 0; DE>DE>} DE>DE>vx = Math.cos(angle) * speed; DE>DE>vy = Math.sin(angle) * speed;DE>
摩擦力应用(简便方法):
DE>vx *= friction; DE>
DE>vy *= friction;
坐标旋转:
1 x1 = Math.cos(angle) * x - Math.sin(angle) * y; y1 = Math.cos(angle) * y + Math.sin(angle) * x;DE>
反坐标旋转:
1 DE>1.DE>DE>x1 = Math.cos(angle) * x + Math.sin(angle) * y; DE>DE>2.DE>DE>y1 = Math.cos(angle) * y - Math.sin(angle) * x;DE>
原文链接: http://blog.csdn.net/ityuany/article/details/6654491