11RIA 闪客社区 - 最赞 Animate Flash 论坛

搜索
查看: 1894|回复: 0
上一主题 下一主题

[高级教程] 【9RIA—沐枫】—【基础小知识】第8节(旋转)

[复制链接] TA的其它主题
发表于 2018-2-6 17:47:41 | 显示全部楼层 |阅读模式

【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
转载:9RIA游戏开发者社区(天地会)
作者:沐枫(原天地会大神)


【基础小知识】系列教程(总目录)——沐枫大神出品


弹簧复位
[Actionscript3] 纯文本查看 复制代码
import flash.events.MouseEvent;

var spring:Number = 0.1;//加速度
var vx:Number = 0;
var vy:Number = 0;
var friction:Number = 0.95;//摩擦力
var springLength:Number = 100;//弹簧长度
//var vg:Number = 0.1;//重力

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
function onEnterFrameHandler(e:Event):void
{
var dx:Number = ball.x - mouseX;
var dy:Number = ball.y - mouseY;
var angle:Number = Math.atan2(dy,dx);
var targetX:Number = mouseX + Math.cos(angle) * springLength;
var targetY:Number = mouseY + Math.sin(angle) * springLength;
vx += (targetX - ball.x) * spring;
vy += (targetY - ball.y) * spring;
vx *= friction;
vy *= friction;
//vy += vg;
ball.x += vx;
ball.y += vy;
graphics.clear();
graphics.lineStyle(4);
graphics.moveTo(ball.x, ball.y);
graphics.lineTo(mouseX, mouseY);
}


弹簧连接多个物体
[Actionscript3] 纯文本查看 复制代码
var ball0:Ball= new Ball();
var ball1:Ball= new Ball();
var ball2:Ball= new Ball();
var ball0Dragging:Boolean = false;
var ball1Dragging:Boolean = false;
var ball2Dragging:Boolean = false;
var spring:Number = 0.1;//加速度
var friction:Number = 0.95;//弹力
var springLength:Number = 100;//弹簧长度

stage.addChild(ball0);
ball0.x = Math.random() * stage.stageWidth;
ball0.y = Math.random() * stage.stageHeight;
ball0.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
ball1.x = Math.random() * stage.stageWidth;
ball1.y = Math.random() * stage.stageHeight;
ball1.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
stage.addChild(ball1);
stage.addChild(ball2);
ball2.x = Math.random() * stage.stageWidth;
ball2.y = Math.random() * stage.stageHeight;
ball2.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);

function onEnterFrameHandler(event:Event):void
{
if (! ball0Dragging)
{
springTo(ball0, ball1);
springTo(ball0, ball2);
}
if (! ball1Dragging)
{
springTo(ball1, ball0);
springTo(ball1, ball2);
}
if (! ball2Dragging)
{
springTo(ball2, ball0);
springTo(ball2, ball1);
}
graphics.clear();
graphics.lineStyle(4);
graphics.moveTo(ball0.x, ball0.y);
graphics.lineTo(ball1.x, ball1.y);
graphics.lineTo(ball2.x, ball2.y);
graphics.lineTo(ball0.x, ball0.y);
}
function springTo(ballA:Ball, ballB:Ball):void
{
var dx:Number = ballA.x - ballB.x;
var dy:Number = ballA.y - ballB.y;
var angle:Number = Math.atan2(dy,dx);
var targetX:Number = ballB.x + Math.cos(angle) * springLength;
var targetY:Number = ballB.y + Math.sin(angle) * springLength;
var vx:Number = 0;
vx += (targetX - ballA.x) * spring;
var vy:Number = 0;
vy += (targetY - ballA.y) * spring;
vx *= friction;
vy *= friction;
ballA.x += vx;
ballA.y += vy;
}
function onPressHandler(event:MouseEvent):void
{
event.target.startDrag();
if (event.target == ball0)
{
ball0Dragging = true;
}
if (event.target == ball1)
{
ball1Dragging = true;
}
if (event.target == ball2)
{
ball2Dragging = true;
}
}
function onReleaseHandler(event:MouseEvent):void
{
ball0.stopDrag();
ball1.stopDrag();
ball0Dragging = false;
ball1Dragging = false;
ball2Dragging = false;
}


气泡效果
[Actionscript3] 纯文本查看 复制代码
package 
{
import flash.display.Sprite;
import flash.events.Event;
public class MF extends Sprite
{
private var balls:Array;
private var numBalls:Number = 30;//气泡数量
private var bounce:Number = -0.5;//弹力
private var spring:Number = 0.05;//加速度
private var gravity:Number = 0.1;//重力系数
public function MF()
{
init();
}
private function init():void
{
balls = new Array();
for (var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = new Ball(Math.random() * 30 + 20,
Math.random() * 0xffffff);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 3;
addChild(ball);
balls.push(ball);
}
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}
private function onEnterFrameHandler(event:Event):void
{
for (var i:uint = 0; i < numBalls - 1; i++)
{
var ball0:Ball = balls[i];
for (var j:uint = i + 1; j < numBalls; j++)
{
var ball1:Ball = balls[j];
var dx:Number = ball1.x - ball0.x;
var dy:Number = ball1.y - ball0.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
var minDist:Number = ball0.radius + ball1.radius;
if (dist < minDist)
{
var angle:Number = Math.atan2(dy,dx);
var tx:Number = ball0.x + Math.cos(angle) * minDist;
var ty:Number = ball0.y + Math.sin(angle) * minDist;
var ax:Number = (tx - ball1.x) * spring;
var ay:Number = (ty - ball1.y) * spring;
ball0.vx -= ax;
ball0.vy -= ay;
ball1.vx += ax;
ball1.vy += ay;
}
}
}
for (i = 0; i < numBalls; i++)
{
var ball:Ball = balls[i];
onMove(ball);
}
}
private function onMove(ball:Ball):void
{
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.x + ball.radius > stage.stageWidth)
{
ball.x = stage.stageWidth - ball.radius;
ball.vx *= bounce;
}
else if (ball.x - ball.radius < 0)
{
ball.x = ball.radius;
ball.vx *= bounce;
}
if (ball.y + ball.radius > stage.stageHeight)
{
ball.y = stage.stageHeight - ball.radius;
ball.vy *= bounce;
}
else if (ball.y - ball.radius < 0)
{
ball.y = ball.radius;
ball.vy *= bounce;
}
}
}
}


多物体旋转
[Actionscript3] 纯文本查看 复制代码
var balls:Array;
var numBalls:uint = 10;//小球数量
var vr:Number = 0.05;//旋转弧度
balls = new Array();
for (var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = new Ball();
balls.push(ball);
addChild(ball);
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
}
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
function onEnterFrameHandler(event:Event):void
{
var angle:Number = (mouseX - stage.stageWidth / 2) * 0.001;//鼠标控制旋转速度
var cos:Number = Math.cos(angle);
var sin:Number = Math.sin(angle);
for (var i:uint = 0; i < numBalls; i++)
{
var ball:Ball = balls[i];
var x1:Number = ball.x - stage.stageWidth / 2;
var y1:Number = ball.y - stage.stageHeight / 2;
var x2:Number = cos * x1 - sin * y1;
var y2:Number = cos * y1 + sin * x1;
ball.x = stage.stageWidth / 2 + x2;
ball.y = stage.stageHeight / 2 + y2;
}
}



下载:
源文件(8).rar (28.22 KB, 下载次数: 7)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

感谢所有支持论坛的朋友:下面展示最新的5位赞助和充值的朋友……更多赞助和充值朋友的信息,请查看:永远的感谢名单

SGlW(66139)、 anghuo(841)、 whdsyes(255)、 longxia(60904)、 囫囵吞澡(58054)

下面展示总排行榜的前3名(T1-T3)和今年排行榜的前3名的朋友(C1-C3)……更多信息,请查看:总排行榜今年排行榜

T1. fhqu1462(969)、 T2. lwlpluto(14232)、 T3. 1367926921(962)  |  C1. anghuo(147)、 C2. fdisker(27945)、 C3. 囫囵吞澡(58054)



快速回复 返回顶部 返回列表