当前位置: 首页 > 图文教程 > Flash动画 > ActionScript > Flash as3.0教程:弹性小球

ActionScript
Flash as入门(4):AS常用语句
Flash as入门(5):学习AS数组
Flash as入门(6):文本与字符串⒒
Flash AS3鼠标事件使用详解
网页中flash的trace方法输出数据
Flash as入门(11):拖动与碰撞检测
AS教程:随机显示数字
Flash AS文本字段的透明度alpha变换
AS教程:对场景和MC添加鼠标监听
AIR设置:transparent和systemChrome
Flash AS3教程:flash.text.TextField
AS教程:理解变量作用域修饰符(modifier)
ActionScript3.0中类间传值问题解决
ActionScript3.0中类的定义以及类属性
Flash AS3教程:类属性的属性
ActionScript3.0教程:变量
ActionScript3.0教程:方法
ActionScript3.0教程:类的枚举
简单认识Flash as面向对象编程
Flash AS2教程:影片剪辑

ActionScript 中的 Flash as3.0教程:弹性小球


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-16   浏览: 56 ::
收藏到网摘: n/a

搞AS游戏开发的朋友也许都听过Keith Peters大师,为我们缔造了那么多的经典算法,偶虽然不是游戏方向,但是还是特别喜欢!近些天拜读了《Making Things Move》的一部分,大师的经典杰作。数学与物理的伟大艺术体现,一直逃不脱我的眼球!

来玩儿一个,做一个健身的小弹力球,在地球上有重力哦^^先看看:




下面是实现:
package{
importflash.display.Sprite;
importflash.events.Event;
importflash.display.StageScaleMode;
importflash.display.Stage;

publicclassTestBallextendsSprite{
privatevarball:Ball;
privatevarspring:Number=0.1;
privatevarvx:Number=0;
privatevarvy:Number=0;
//这个是摩擦力啦!可以调节一下值看看变化
privatevarfriction:Number=0.95;
//这个是重力,和我们现实生活中差不多
privatevargravity:Number=5;
publicfunctionTestBall(){
this.stage.scaleMode=StageScaleMode.NO_SCALE;
//实例化一个小球(在库里的哦)
ball=newBall();
this.addChild(ball);
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
privatefunctiononEnterFrame(event:Event):void{
//下面是核心算法,好好研究哦
vardx:Number=mouseX-ball.x;
vardy:Number=mouseY-ball.y;
varax:Number=dx*spring;
varay:Number=dy*spring;
vx =ax;
vy =ay;
vy =gravity;
vx*=friction;
vy*=friction;
ball.x =vx;
ball.y =vy;
//绘制线条
this.graphics.clear();
this.graphics.lineStyle(1);
this.graphics.moveTo(ball.x,ball.y);
this.graphics.lineTo(mouseX,mouseY);
}
}
}
-*-原文地址:http://fck.name/node/138,转载请注明出处,谢谢!-*-