# Flash/Flex学习笔记(40)：弹性运动续--弹簧

var ball:Ball = new Ball(6);
ball.y = 20;
ball.x = 20;

var targetX:Number=stage.stageWidth/2;
var targetY:Number=ball.y;

var springLength = 100;//弹簧长度
var spring = 0.2;//弹性系数
var friction = 0.92;//摩擦系数

//画辅助线，以便看得更清楚
graphics.lineStyle(0.5,0xaaaaaa);
graphics.moveTo(ball.x,ball.y);
graphics.lineTo(stage.stageWidth-ball.x,ball.y);
graphics.moveTo(targetX,targetY-10);
graphics.lineTo(targetX,targetY+10);
graphics.moveTo(targetX-springLength,targetY-8);
graphics.lineTo(targetX-springLength,targetY+8);

var rect:Rectangle = new Rectangle(ball.x,ball.y,stage.stageWidth-ball.x*2,0);

function EnterFrameHandler(e:Event):void{
ball.vx += (targetX - springLength - ball.x)*spring;
ball.vx *= friction;
ball.x += ball.vx;
}

function MouseDownHandler(e:MouseEvent):void{
(e.target as Sprite).startDrag(true,rect);
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);
}

function MouseUpHandler(e:MouseEvent):void{
ball.stopDrag();
}



var ball:Ball = new Ball(10);
ball.y = 20;
ball.x = 20;

var targetX:Number=stage.stageWidth/2;
var targetY:Number=stage.stageHeight/2;

var springLength:uint = 100;//弹簧长度
var spring:Number = 0.2;//弹性系数
var friction:Number = 0.92;//摩擦系数
var angle:Number = 0;

angle = Math.atan2(targetY - ball.y,targetX -ball.x);//确定夹角
trace(angle*180/Math.PI);

function EnterFrameHandler(e:Event):void{
ball.vx += (targetX - springLength*Math.cos(angle) - ball.x)*spring;//调整目标点
ball.vy += (targetY - springLength*Math.sin(angle) - ball.y)*spring;
ball.vx *= friction;
ball.vy *= friction;
ball.x += ball.vx;
ball.y += ball.vy;
DrawLine();
}

function DrawLine():void{
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(targetX,targetY-10);
graphics.lineTo(targetX,targetY+10);
graphics.moveTo(targetX-10,targetY);
graphics.lineTo(targetX+10,targetY);
graphics.moveTo(targetX,targetY);
graphics.lineStyle(0.5,0xaaaaaa);
graphics.lineTo(ball.x,ball.y);
}

function MouseDownHandler(e:MouseEvent):void{
(e.target as Sprite).startDrag(true);
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);
}

function MouseUpHandler(e:MouseEvent):void{
ball.stopDrag();
}



function EnterFrameHandler(e:Event):void{
targetX = mouseX;//改成动态目标
targetY = mouseY;
angle = Math.atan2(targetY - ball.y,targetX -ball.x);//动态夹角
ball.vx += (targetX - springLength*Math.cos(angle) - ball.x)*spring;
ball.vy += (targetY - springLength*Math.sin(angle) - ball.y)*spring;
ball.vx *= friction;
ball.vy *= friction;
ball.x += ball.vx;
ball.y += ball.vy;
DrawLine();
}


var ball_1:Ball = new Ball(10,0xff0000);
var ball_2:Ball = new Ball(10,0x0000ff);
ball_1.x = stage.stageWidth * Math.random();
ball_1.y = stage.stageHeight * Math.random();
ball_2.x = stage.stageWidth/2;
ball_2.y = stage.stageHeight/2;

var spring:Number = 0.1;
var springLength:uint = 100;
var friction:Number = 0.9;
var darggingBall:Ball;

function MouseOutHandler(e:MouseEvent){
Mouse.cursor = MouseCursor.AUTO;
}

function MouseOverHandler(e:MouseEvent){
Mouse.cursor = MouseCursor.HAND;
}

function MouseDownHandler(e:MouseEvent):void{
(e.target as Sprite).startDrag(true,new Rectangle(20,20,stage.stageWidth-40,stage.stageHeight-40));
darggingBall = e.target as Ball;
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);
}

function MouseUpHandler(e:MouseEvent):void{
if (darggingBall!=null){
darggingBall.stopDrag();
darggingBall = null;
}
}

function EnterFrameHandler(e:Event):void{
var dx1 = ball_2.x -ball_1.x;
var dy1 = ball_2.y -ball_1.y;
var angle1:Number = Math.atan2(dy1,dx1);
ball_1.vx += (ball_2.x - springLength * Math.cos(angle1) - ball_1.x) * spring;
ball_1.vy += (ball_2.y - springLength * Math.sin(angle1) - ball_1.y) * spring;
ball_1.vx *= friction;
ball_1.vy *= friction;
ball_1.x += ball_1.vx;
ball_1.y += ball_1.vy;

var dx2 = ball_1.x -ball_2.x;
var dy2 = ball_1.y -ball_2.y;
var angle2:Number = Math.atan2(dy2,dx2);
ball_2.vx += (ball_1.x - springLength * Math.cos(angle2) - ball_2.x) * spring;
ball_2.vy += (ball_1.y - springLength * Math.sin(angle2) - ball_2.y) * spring;
ball_2.vx *= friction;
ball_2.vy *= friction;
ball_2.x += ball_2.vx;
ball_2.y += ball_2.vy;

DrawLine();

CheckBoundary(ball_1);
CheckBoundary(ball_2);

}

function DrawLine():void{
graphics.clear();
graphics.lineStyle(0.5,0x666666);
graphics.moveTo(ball_1.x,ball_1.y);
graphics.lineTo(ball_2.x,ball_2.y);
}

function CheckBoundary(b:Ball){
if (b.x>stage.stageWidth-b.width/2 || b.x<=b.width/2){
b.x -= b.vx;
b.vx *= -1;
}

if (b.y>stage.stageHeight-b.height/2 || b.y<=b.height/2){
b.y -= b.vy;
b.vy *= -1;
}
}


var spring:Number=0.1;
var springLength:uint=150;
var friction:Number=0.8;//摩擦力
var darggingBall:Ball;
var ballNumber:uint = 3;//小球个数

var arrBalls:Array = new Array(ballNumber);

for(var i:uint=0,j=arrBalls.length;i<j;i++){
arrBalls[i] = new Ball(20,Math.random() * 0xffffff);
var _ball:Ball = arrBalls[i];
_ball.x=stage.stageWidth*Math.random();
_ball.y=stage.stageHeight*Math.random();
}

//切换光标
function MouseOutHandler(e:MouseEvent) {
Mouse.cursor=MouseCursor.AUTO;
}

//切换光标
function MouseOverHandler(e:MouseEvent) {
Mouse.cursor=MouseCursor.HAND;
}

//开始拖动
function MouseDownHandler(e:MouseEvent):void {
(e.target as Sprite).startDrag(true,new Rectangle(20,20,stage.stageWidth-40,stage.stageHeight-40));
darggingBall=e.target as Ball;
removeEventListener(Event.ENTER_FRAME,EnterFrameHandler);
}

//结束拖动
function MouseUpHandler(e:MouseEvent):void {
if (darggingBall!=null) {
darggingBall.stopDrag();
darggingBall=null;
}
}

function EnterFrameHandler(e:Event):void {

for(var i:uint=0,j=arrBalls.length-1;i<j;i++){
SpringTo(arrBalls[i],arrBalls[i+1]);
}
SpringTo(arrBalls[arrBalls.length-1],arrBalls[0]);

DrawLine();

for(i=0,j=arrBalls.length;i<j;i++){
CheckBoundary(arrBalls[i]);
}

}

//画连接线
function DrawLine():void {
graphics.clear();
graphics.lineStyle(0.5,0x666666);
//graphics.moveTo(ball_1.x,ball_1.y);
//graphics.lineTo(ball_2.x,ball_2.y);

for(var i:uint=0,j=arrBalls.length-1;i<j;i++){
graphics.moveTo(arrBalls[i].x,arrBalls[i].y);
graphics.lineTo(arrBalls[i+1].x,arrBalls[i+1].y);
}
graphics.lineTo(arrBalls[0].x,arrBalls[0].y);
}

//弹性运动处理
function SpringTo(targetBall:Ball,moveBall:Ball):void{
var dy=targetBall.y-moveBall.y;
var dx=targetBall.x-moveBall.x;
var angle1:Number=Math.atan2(dy,dx);
moveBall.vx += (targetBall.x - springLength * Math.cos(angle1) - moveBall.x) * spring;
moveBall.vy += (targetBall.y - springLength * Math.sin(angle1) - moveBall.y) * spring;
moveBall.vx *= friction;
moveBall.vy *= friction;
moveBall.x += moveBall.vx;
moveBall.y += moveBall.vy;
}

//检测边界
function CheckBoundary(b:Ball) {
if (b.x>stage.stageWidth-b.width/2||b.x<=b.width/2) {
b.x-=b.vx;
b.vx*=-1;
}

if (b.y>stage.stageHeight-b.height/2||b.y<=b.height/2) {
b.y-=b.vy;
b.vy*=-1;
}
}


|
10月前
|

42 0
|
Java 程序员 容器
Flex弹性盒子（一篇带你掌握潮流 Flex 布局）（下）
5.当切换容器排列方式后，水平对齐以及垂直对齐的 应用 变化（重点、难点⭐⭐⭐⭐⭐） 当使用flex-direction: (例 column) ;时 justify-content 、 align-content 、align-items发生的变化 当排列为column / column-reverse 时，其实容器的主轴发生了变化 主轴现在是竖轴，副轴是横轴
110 0
|

Flex弹性盒子（一篇带你掌握潮流 Flex 布局）（上）
Flex弹性盒子 1.说在前头 1.1.Flex布局出现之前 我们知道传统的页面布局依赖于 盒状模型 依赖于display、position、float属性 对于某些特殊布局来说特别不方便，比如垂直居中（比较不容易实现） 1.2.Flex布局出现后 2009年，W3C提出Flex布局 简便、完整、响应式的各种实现页面布局（目前已得到所有主流浏览器的支持）
279 0
Photoshop - PS/AE渐变工具出现色带波纹色彩过渡不均匀咋办？
Photoshop - PS/AE渐变工具出现色带波纹色彩过渡不均匀咋办？
978 0
|

|
XML 数据格式 内存技术
|

Flex:地图缩放平移效果(简易版)

972 0
|

Flash/Flex学习笔记(55)：背面剔除与 3D 灯光
Animation in ActionScript3.0 这本书总算快学完了，今天继续：上一回Flash/Flex学习笔记(50)：3D线条与填充 里，我们知道任何一个3D多面体上的某一个面，都可以分解为多个三角形的组合。
920 0
|

Flash/Flex学习笔记(56)：矩阵变换

851 0