as3种常见的弹性效果公式以及波形运动等as3动画效果公式代码整理,对于常用的来说作者整理的很全面,包括AS3的进制转换颜色提取等效果:
AS3缓动公式:
- sprite.x += (targetX – sprite.x) * easing;//easing为缓动系数变量sprite.y += (targetY – sprite.y) * easing;
复制代码
AS3弹性公式:
- vx += (targetX – sprite.x) * spring;//spring为弹性系数vy += (targetY – sprite.y) * spring;sprite.x += (vx *= friction);//friction为摩擦力sprite.y += (vy *= friction);
复制代码
AS3偏移弹性公式:
- 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向鼠标旋转(或向某点旋转)
- dx = mouseX – sprite.x;dy = mouseY – sprite.y;sprite.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
复制代码
AS3波形运动:
- 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;}
复制代码
AS3圆心旋转:
- 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;}
复制代码
AS3颜色运算得到透明值:
- var t:uint=0×77ff8877var s:uint=0xff000000var h:uint=t&svar m:uint=h>>>24trace(m)
复制代码
AS3转换为十进制: trace(hexValue);
AS3十进制转换为十六进制:
- decimalValue.toString(16)
复制代码
AS3颜色提取:
- 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按位计算得到颜色值:
- color24 = red < < 16 | green << 8 | blue;color32 = alpha << 24 | red << 16 | green << 8 | blue;
复制代码
AS3过控制点的曲线:
- // 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右键菜单: 建立右键菜单项:
- 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))
复制代码
常用事件:
- cm.addEventListener(ContextMenuEvent.MENU_SELECT,function(){trace(“ss”)})//当右键菜单触发时cm.customItems[i].addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,MenuItemSelectHandler);//当某个选择项触发时e.currentTarget.caption;得到相应的caption
复制代码
----------------------------------------------------------- 缓动和弹性
简单缓动:
- // 长形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;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 = fixedY + Math.sin(angle) * springLength;
复制代码
碰撞检测
距离碰撞检测:
- //从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) {//处理碰撞}
复制代码
多物体碰撞检测:
- 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 之间进行碰撞检测}}
复制代码
坐标旋转和角度回弹
坐标旋转:
- x1 = Math.cos(angle) * x - Math.sin(angle) * y;y1 = Math.sin(angle) * y + Math.cos(angle) * x;
复制代码
反坐标旋转:
- 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²)
动量守恒的数学表达式:
- v0Final = ((m0 - m1) * v0 + 2 * m1 * v1 ) / (m0 + m1)v1Final = ((m1 - m0) * v1 + 2 * m0 * v0 ) / (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;
复制代码
粒子(Particles)引力和重力
引力的一般公式:
- force = G * m1 * m2 / distance²
- 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速度向量:
- vx = speed * Math.cos(angle);vy = speed * Math.sin(angle);
复制代码
角加速度(作用于物体上的force)转换为x,y加速度:
- ax = force * Math.cos(angle);ay = force * Math.sin(angle);
复制代码
将加速度加入速度向量:
将速度向量加入坐标:
- sprite.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 = 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;}
复制代码
摩擦力应用(正确方法):
- 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;
复制代码
———————————————————– 缓动公式:
- 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;
- }
复制代码
按位计算得到颜色值:
- color24 = red << 16 | green << 8 | blue;
- color32 = alpha << 24 | red << 16 | green << 8 | blue;
复制代码
过控制点的曲线:
- // 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);
- 向鼠标旋转(或向某点旋转)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;}
复制代码
勾股定理
- 两个点坐标分别为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是开平方的意思
复制代码
画布中随机坐标
- sprite1.x = Math.random() * stage.stageWidth;sprite1.y = Math.random() * stage.stageHeight;
复制代码
鼠标到Sprite之间连线
- 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
计算两点间距离:
- dx = x2 – x1;dy = y2 – y1;dist = Math.sqrt(dx*dx + dy*dy);
复制代码
偏移弹性公式:
- 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;
复制代码
过控制点的曲线:
- // 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);
复制代码
// 重置影片的位置和速度
- 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){ }
复制代码
摩擦力应用(正确方法):
- 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; 坐标旋转:
- x1 = Math.cos(angle) * x - Math.sin(angle) * y; y1 = Math.cos(angle) * y + Math.sin(angle) * x;DE>
复制代码
反坐标旋转:
- 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 |