package { import flash.display.Sprite; import flash.events.Event; import flash.display.StageAlign; import flash.display.StageScaleMode; public class MultiBounce3D extends Sprite { private var balls:Array; private var numBalls:uint=20; private var fl:Number=250; private var vpX:Number=stage.stageWidth/2; private var vpY:Number=stage.stageHeight/2; private var top:Number=-120; private var bottom:Number=120; private var left:Number=-120; private var right:Number=120; private var front:Number=120; private var back:Number=-120; public function MultiBounce3D() { init(); } private function init():void { stage.align=StageAlign.TOP_LEFT; stage.scaleMode=StageScaleMode.NO_SCALE; balls = new Array(); for (var i:uint = 0; i < numBalls; i++) { var ball:Ball3D=new Ball3D(15,Math.random()*0xffffff); balls.push(ball); ball.vx=Math.random()*10-5; ball.vy=Math.random()*10-5; ball.vz=Math.random()*10-5; addChild(ball); } addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { vpX=stage.stageWidth/2; vpY=stage.stageHeight/2; graphics.clear(); for (var i:uint = 0; i < numBalls; i++) { var ball:Ball3D=balls[i]; move(ball); } sortZ(); } function sortZ():void { balls.sortOn("zpos", Array.DESCENDING | Array.NUMERIC); for (var i:uint = 0; i < numBalls; i++) { var ball:Ball3D=balls[i]; setChildIndex(ball, i); } } private function move(ball:Ball3D):void { var radius:Number=ball.radius; ball.xpos+=ball.vx; ball.ypos+=ball.vy; ball.zpos+=ball.vz; //6边界检测 if (ball.xpos+radius>right) { ball.xpos=right-radius; ball.vx*=-1; } else if (ball.xpos - radius < left) { ball.xpos=left+radius; ball.vx*=-1; } if (ball.ypos+radius>bottom) { ball.ypos=bottom-radius; ball.vy*=-1; } else if (ball.ypos - radius < top) { ball.ypos=top+radius; ball.vy*=-1; } if (ball.zpos+radius>front) { ball.zpos=front-radius; ball.vz*=-1; } else if (ball.zpos - radius < back) { ball.zpos=back+radius; ball.vz*=-1; } //转换化2D坐标 if (ball.zpos>- fl) { var scale:Number = fl / (fl + ball.zpos); ball.scaleX=ball.scaleY=scale; ball.x=vpX+ball.xpos*scale; ball.y=vpY+ball.ypos*scale; ball.visible=true; } else { ball.visible=false; } //辅助线 graphics.lineStyle(1,0xccccff); graphics.moveTo(0,stage.stageHeight/2); graphics.lineTo(stage.stageWidth,stage.stageHeight/2); graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2-8); graphics.moveTo(stage.stageWidth,stage.stageHeight/2); graphics.lineTo(stage.stageWidth-15,stage.stageHeight/2+8); graphics.moveTo(0,stage.stageHeight); graphics.lineTo(stage.stageWidth,0); graphics.lineTo(stage.stageWidth-15,2); graphics.moveTo(stage.stageWidth,0); graphics.lineTo(stage.stageWidth-6,13); graphics.moveTo(stage.stageWidth/2,0); graphics.lineTo(stage.stageWidth/2,stage.stageHeight); graphics.lineTo(stage.stageWidth/2-8,stage.stageHeight-15); graphics.moveTo(stage.stageWidth/2,stage.stageHeight); graphics.lineTo(stage.stageWidth/2+8,stage.stageHeight-15); graphics.lineStyle(1,0xff99ff); graphics.moveTo(vpX,vpY); graphics.lineTo(ball.x,ball.y); } } }