http://linfuqing.wsu.wsu.cn/zerO/Buildparticles.swf
/**
* Copyright 2009 (c) Lin Fuqing,zerO3D
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package
{
import flash.display.Sprite;
import flash.events.Event;
import zerO.objects.Track;
import zerO.particles.simplesprite.Ball;
import zerO.render.Zsort;
import zerO.scenes.Scene3D;
import zerO.veiw.Camera3D;
import zerO.veiw.Viewport3D;
public class BuildParticles extends Sprite
{
//追踪,一个为懒人设置的类,可以不用.
private var track:Track;
//相机,可以不用.
private var camera:Camera3D = new Camera3D();
//半径,球分散的半径
private var radius:Number;
//球的数量
private var numBalls:uint;
//场景,必须
private var scene:Scene3D = new Scene3D();
//视口,必须
private var viewport:Viewport3D = new Viewport3D( stage.stageWidth,stage.stageHeight,scene );
//渲染器,必须,这里采用ZSORT,方便又便捷,娃哈哈.
private var render:Zsort = new Zsort( viewport );
/**
* 粒子系统实例
*/
public function BuildParticles( numBalls:uint = 500,radius:Number = 200 )
{
//定义新追踪,必须传最上层的精灵上去,否则无效.
track = new Track(this);
//视口连接相机,后面还有个参数是相机切换插值,使用可以生成很华丽的动画
viewport.onCamera( camera );
//将视口加入子对象,一定要加,不然你将看不到任何东西
addChild( viewport );
//初始化
this.numBalls = numBalls;
this.radius = radius;
init();
//初始化完成后记得场景build一下,谢谢合作
scene.build();
addEventListener( Event.ENTER_FRAME,onEnterFrame );
}
/**
* 初始化,这里定义numBalls个球,加到场景中
*/
public function init():void
{
for( var i:uint = 0;i < numBalls;i++ )
{
var ball:Ball = new Ball(5,Math.random() * 0xffffff);
ball.x = Math.random() * radius * 2 - radius;
ball.y = Math.random() * radius * 2 - radius;
ball.z = Math.random() * radius * 2 - radius;
scene.add( ball );//记得定义完一定要加到场景里面
}
}
/**
* OK,开始
*/
public function onEnterFrame( event:Event ):void
{
track.cameraTrueDirection( camera );//按鼠标追踪相机真实方向,玩过CS么,你也可以不用,直接设置相机方向
for( var i:uint = 0;i < numBalls;i++ )
{
var ball:Ball = scene.renderList[i];
ball.rotateY(1);//将ball进行绕Y轴旋转
}
render.render();//渲染机渲染(必须).
}
}
}