来玩儿一个,做一个健身的小弹力球,在地球上有重力哦^^先看看:
下面是实现:
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,转载请注明出处,谢谢!-*-