【游客模式】——注册会员,加入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)
|