賽車程式

本文介绍了一个使用ActionScript和Papervision3D库创建的3D场景案例。该案例实现了基本的3D跑道环境,并通过键盘输入控制3D空间中对象的移动和旋转。此外,还展示了如何在2D平面上绘制3D场景的地图。
摘要由CSDN通过智能技术生成
package { import flash.display.*; import flash.events.*; import flash.geom.*; import flash.utils.*; import org.papervision3d.cameras.*; import org.papervision3d.materials.*; import org.papervision3d.objects.*; import org.papervision3d.scenes.*; import flash.ui.Keyboard; [SWF(backgroundColor="#000000", frameRate="60", width="400", height="400")] public class T6step1 extends Sprite { private static var PI:Number = Math.PI; private static var TH:Number = PI / 180; private var container:Sprite; private var scene:MovieScene3D; private var camera:Camera3D; private var map:Sprite; private var nextPoint:DisplayObject3D; private var keyCodeDown:Array = null; public function T6step1() { //背景 var bg:Sprite = new Sprite(); addChild(bg); bg.graphics.beginFill(0x999999); bg.graphics.drawRect(0, 0, 400, 400); bg.graphics.endFill(); // initialize the objects init3D(); // add a listener for the 3D loop addEventListener(Event.ENTER_FRAME, loop3D); //地圖 map = new Sprite(); addChild(map); map.x = 200; map.y = 200; stage.focus = this; this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDownHandler); this.stage.addEventListener(KeyboardEvent.KEY_UP, this.keyUpHandler); } private function init3D():void{ container = new Sprite(); addChild(container); container.x = 200; container.y = 200; scene = new MovieScene3D(container); camera = new Camera3D(); camera.zoom = 15; camera.focus = 100; //跑道 var newX:Number; var newZ:Number; for(var i:int=0; i<360; i+=10){ newX = 900 * formatNum(Math.cos(i*TH), 3); newZ = 900 * formatNum(Math.sin(i*TH), 3); scene.addChild(new Cone(new ColorMaterial(0xffcc00), 5, 30, 8, 6, {x:newX, z:newZ}), "myCone1_" + i); newX = 800 * formatNum(Math.cos(i*TH), 3); newZ = 800 * formatNum(Math.sin(i*TH), 3); scene.addChild(new Cone(new ColorMaterial(0xffcc00), 5, 30, 8, 6, {x:newX, z:newZ}), "myCone2_" + i); } //移動控點 nextPoint = scene.addChild(new DisplayObject3D(), "nextPoint"); } private function loop3D(event:Event):void{ //camera.hover(0, container.mouseX / 20, container.mouseY / 10); if(keyCodeDown!=null){ var isMoving:Boolean = false; for(var i:int=0; i<keyCodeDown.length; i++){ if(keyCodeDown[i]==Keyboard.UP){ nextPoint.moveForward(30); isMoving = true; }else if(keyCodeDown[i]==Keyboard.DOWN){ nextPoint.moveBackward(25); isMoving = true; } } if(isMoving){ for(var i:int=0; i<keyCodeDown.length; i++){ if(keyCodeDown[i]==Keyboard.LEFT){ nextPoint.rotationY-=5; }else if(keyCodeDown[i]==Keyboard.RIGHT){ nextPoint.rotationY+=5; } } } } camera.target = nextPoint; camera.x = nextPoint.x; camera.y = nextPoint.y; camera.z = nextPoint.z; camera.rotationY = nextPoint.rotationY; camera.moveBackward(200); camera.moveUp(30); //camera.lookAt(nextPoint); scene.renderCamera(camera); //地圖 map.graphics.clear(); map.graphics.lineStyle(1); map.graphics.drawRect(-50, -50, 100, 100); //方塊群 var myCone:DisplayObject3D; for(var i:int=0; i<360; i+=10){ myCone = scene.getChildByName("myCone1_" + i); map.graphics.drawCircle((myCone.x)/20, (myCone.z)/20, 1); myCone = scene.getChildByName("myCone2_" + i); map.graphics.drawCircle((myCone.x)/20, (myCone.z)/20, 1); } map.graphics.lineStyle(1, 0xff0000); map.graphics.drawCircle((nextPoint.x)/20, -1*(nextPoint.z)/20, 1); } public function keyDownHandler(event:KeyboardEvent):void{ trace(event.keyCode); if(keyCodeDown==null){ keyCodeDown = new Array(); keyCodeDown.push(event.keyCode); }else{ for(var i:int=0; i<keyCodeDown.length; i++){ if(keyCodeDown[i]==event.keyCode){ return; } keyCodeDown.push(event.keyCode); } } } public function keyUpHandler(event:KeyboardEvent):void{ if(keyCodeDown!=null){ for(var i:int=keyCodeDown.length-1; i>=0; i--){ if(keyCodeDown[i]==event.keyCode){ keyCodeDown.splice(i, 1); } } if(keyCodeDown.length==0){ keyCodeDown = null; } } } private function formatNum(num:Number, cnt:Number):Number{ return Math.round(num * Math.pow(10, cnt)) / Math.pow(10, cnt); } }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值