【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
转载:9RIA游戏开发者社区(天地会)
作者:沐枫(原天地会大神)
【基础小知识】系列教程(总目录)——沐枫大神出品
摩擦力
[Actionscript3] 纯文本查看 复制代码 import flash.events.MouseEvent;
var vx:Number = 0;
var vy:Number = 0;
//var friction:Number = 0.1;//定义一个摩擦力
var friction:Number = 0.9;//不精确摩擦
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(MouseEvent.CLICK,onClick);
function onClick(e:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
vx=(mouseX-ball.x)/20;
vy=(mouseY-ball.y)/20;//初始速度
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}
function onEnterFrameHandler(e:Event):void
{
//var speed:Number= Math.sqrt(vx * vx + vy * vy);
// if (speed > friction)
// {
// speed -= friction;
// }
// else
// {
// speed = 0;
// removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
// }
// var angle:Number = Math.atan2(vy,vx);
//vx = Math.cos(angle) * speed;
// vy = Math.sin(angle) * speed;
vx*=friction;
vy*=friction;
ball.x += vx;
ball.y += vy;
}
移出对象
[Actionscript3] 纯文本查看 复制代码 var count:int = 10;
var balls:Array;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;//舞台的尺寸与播放器尺寸相匹配
balls=new Array();
var vx:Number = 0;
var vy:Number = 0;
vx = Math.random() * 2 - 1;
vy = Math.random() * 2 - 1;
for (var i:int=0; i < count; i++)
{
var ball:Ball = new Ball();
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random() * stage.stageHeight;
stage.addChild(ball);
balls.push(ball);
}
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
function onEnterFrameHandler(event:Event):void
{
for (var i:Number=balls.length -1; i > -1; i--)
{
balls[i].x += vx;
balls[i].y += vy;
if (balls[i].x - balls[i].width / 2 > stage.stageWidth || balls[i].x + balls[i].width / 2 < 0 || balls[i].y - balls[i].width / 2 > stage.stageHeight || balls[i].y + balls[i].width / 2 < 0)
{
stage.removeChild(balls[i]);
balls.splice(i,1);
if (balls.length <= 0)
{
stage.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
}
}
}
}
重置对象(喷泉效果)
[Actionscript3] 纯文本查看 复制代码 var count:int = 500;
var balls:Array;
var gravity:Number = 0.5;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;//舞台的尺寸与播放器尺寸相匹配
balls=new Array();
var vx:Number = 0;
var vy:Number = 0;
vx = Math.random() * 2 - 1;
vy = Math.random() * -10 - 10;
for (var i:int=0; i < count; i++)
{
var ball:Ball = new Ball();
ball.x = 275;
ball.y = stage.stageHeight;
stage.addChild(ball);
balls.push(ball);
}
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
function onEnterFrameHandler(event:Event):void
{
for (var i:Number = 0; i < 500; i++)
{
vy += gravity;//加速度
vx+=0.01;//模拟风力
balls[i].x += vx;
balls[i].y += vy;
if (balls[i].x - balls[i].width/2 > stage.stageWidth ||balls[i].x + balls[i].width/2 < 0 ||balls[i].y - balls[i].width/2 > stage.stageHeight ||balls[i].y +balls[i].width/2 < 0)
{
balls[i].x = 275;
balls[i].y = stage.stageHeight;
vx = Math.random() * 2 - 1;
vy = Math.random() * -10 - 10;
}
}
}
反弹
[Actionscript3] 纯文本查看 复制代码 import flash.events.Event;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;//设置舞台大小与播放窗口大小同步
var bounce:Number = 0.7;//弹力系数
var vy:Number = 4;//下落速度
var g:Number=0.06;//重力系数
stage.addEventListener(Event.ENTER_FRAME,onMc);
function onMc(e:Event):void
{
mc.y += vy;
vy+=g;
if (mc.y + mc.width / 2 > stage.stageHeight)
{
vy *= -bounce;
}
}
下载:
源文件(六).rar
(28.01 KB, 下载次数: 3)
|