今天上网看见一个网站上这个效果,下面还写了一个公式,好像是高二学过的万有引力,没记住,忘了。现在自己模拟一下,主要是粒子与舞台边缘的碰撞检测以及粒子自身之间距离的计算:
代码如下:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author aisajiajiao
*/
[SWF(backgroundColor="0x000000",width="800",height="600")]
public class Main extends Sprite
{
private static const RADIUS_MAX:Number = 50;
private static const MIN_DIST = 100;
private var ballArray:Array = [];
private var ballNumber:Number = 100;
public function Main()
{
initBalls();
stage.addEventListener(Event.ENTER_FRAME,moveBalls);
}
private function initBalls():void
{
for(var i:int = 0;i < ballNumber;i++)
{
var ball:Ball = new Ball(RADIUS_MAX*Math.random());
ball.x = stage.stageWidth * Math.random();
ball.y = stage.stageHeight * Math.ran