拥有重力的弹性小球(as3.0)

下面是实现:

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.Stage;

public class TestBall extends Sprite {
private var ball:Ball;
private var spring:Number=0.1;
private var vx:Number=0;
private var vy:Number=0;
//这个是摩擦力啦!可以调节一下值看看变化
private var friction:Number=0.95;
//这个是重力,和我们现实生活中差不多
private var gravity:Number=5;
public function TestBall() {
this.stage.scaleMode=StageScaleMode.NO_SCALE;
//实例化一个小球(在库里的哦)
ball=new Ball();
this.addChild(ball);
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(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;
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);
}
}
}

 

阅读更多
个人分类: actionscript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭