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

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

[高级教程] 【9RIA—沐枫】—【基础小知识】第7节(弹性与缓动)

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

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

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

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


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



按钮链接
[Actionscript3] 纯文本查看 复制代码
import flash.events.MouseEvent;
import flash.net.URLRequest;

btn.addEventListener(MouseEvent.CLICK,onMouseClickHandler)
function onMouseClickHandler(e:MouseEvent):void
{
navigateToURL(new URLRequest("http://bbs.9ria.com/thread-163247-1-1.html"),"_blank")
}


多目标弹性
[Actionscript3] 纯文本查看 复制代码
var ball:Ball;
var handles:Array;
var spring:Number = 0.1;//加速度
var friction:Number = 0.8;//摩擦力
var numHandles:Number = 3;//控制点小球数
var vx:Number=0;
var vy:Number=0;
ball = new Ball();
addChild(ball);
handles = new Array();
for (var i:uint = 0; i < numHandles; i++)
{
var handle:Ball = new Ball();
handle.x = Math.random() * stage.stageWidth;
handle.y = Math.random() * stage.stageHeight;
handle.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
addChild(handle);
handles.push(handle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);

function onEnterFrameHandler(event:Event):void
{
for (var i:uint = 0; i < numHandles; i++)
{
var handle:Ball = handles[i] as Ball;//转换为Ball类型
var dx:Number = handle.x - ball.x;
var dy:Number = handle.y - ball.y;
vx += dx * spring;
vy += dy * spring;
}
vx *= friction;
vy *= friction;
ball.x += vx;
ball.y += vy;
graphics.clear();
graphics.lineStyle(1);
for (i = 0; i < numHandles; i++)
{
graphics.moveTo(ball.x, ball.y);
graphics.lineTo(handles[i].x, handles[i].y);
}
}
function onPressHandler(event:MouseEvent):void
{
event.target.startDrag();
}
function onReleaseHandler(event:MouseEvent):void
{
stopDrag();
}


二维弹性运动
[Actionscript3] 纯文本查看 复制代码
var spring:Number = 0.1;
var vx:Number = 50;
var vy:Number = 0;
var friction:Number = 0.95;
var gravity:Number = 5;

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
function onEnterFrameHandler(event:Event):void
{
var dx:Number = mouseX - ball.x;
var dy:Number = mouseY - ball.y;
var ax:Number = dx * spring;//加速度
var ay:Number = dy * spring;
vx += ax;
vy += ay;
vx *= friction;//摩擦力作用
vy *= friction;
vy += gravity;//重力
ball.x += vx;
ball.y += vy;
graphics.clear();
graphics.lineStyle(1);
graphics.moveTo(ball.x, ball.y);
graphics.lineTo(mouseX, mouseY);
}


缓动
[Actionscript3] 纯文本查看 复制代码
var easing:Number = 0.2;//速度/距离
ball.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);

function onMouseDownHandler(event:MouseEvent):void
{
ball.startDrag();
removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
}
function onMouseUpHandler(event:MouseEvent):void
{
ball.stopDrag();
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
}
function onEnterFrameHandler(event:Event):void
{
if (Math.abs(mouseX - ball.x)<1&&Math.abs(mouseY - ball.y)<1)//因为Sprite每帧做少移动像素为1/20,所以不精确计算
{
ball.x=mouseX;
ball.y=mouseY;
removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
trace("停止");
}
else
{
var vx:Number = (mouseX - ball.x) * easing;
var vy:Number = (mouseY - ball.y) * easing;
ball.x += vx;
ball.y += vy;
}
}


投掷
[Actionscript3] 纯文本查看 复制代码
import flash.events.Event;

var vx:Number = 0;
var vy:Number = 0;
var bounce:Number = -0.8;//弹力系数
var gravity:Number = 0.5;//重力加速度
var oldX:Number = 0;//小球初始位置
var oldY:Number = 0;
var vz:Number=0.99;//阻力
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
ball.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
vx = Math.random() * 10 - 5;
vy = -10;
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
function onEnterFrameHandler(event:Event):void
{
vx*=vz;
vy*=vz;
vy += gravity;
ball.x += vx;
ball.y += vy;
var left:Number = 0;
var right:Number = stage.stageWidth;
var top:Number = 0;
var bottom:Number = stage.stageHeight;
if (ball.x + ball.width / 2 > right)
{
ball.x = right - ball.width / 2;
vx *= bounce;
}
else if (ball.x - ball.width/2 < left)
{
ball.x = left + ball.width / 2;
vx *= bounce;
}
if (ball.y + ball.width / 2 > bottom)
{
ball.y = bottom - ball.width / 2;
vy *= bounce;
}
else if (ball.y - ball.width/2 < top)
{
ball.y = top + ball.width / 2;
vy *= bounce;
}
}
function onMouseDownHandler(event:MouseEvent):void
{
vx = 0;
vy = 0;
oldX = ball.x;
oldY = ball.y;
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
ball.startDrag();
removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
addEventListener(Event.ENTER_FRAME,onTake);
}
function onTake(e:Event):void
{
vx = ball.x - oldX;
vy = ball.y - oldY;
oldX = ball.x;
oldY = ball.y;
}
function onMouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
ball.stopDrag();
removeEventListener(Event.ENTER_FRAME,onTake);
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}



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

本版积分规则

关闭

站长推荐 上一条 /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)



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