Sealyu

--- 博客已迁移至: http://www.sealyu.com/blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 Trackbacks
1、修改PaperBase基类,存贮新的相机、场景和视图区,然后重新渲染产生新的效果。因为,首先需要定义三个新的类成员属性:

 

 public var current_scene:Scene3D;//为实现3D键盘交互准备
 public var current_camera:CameraObject3D;//为实现3D键盘交互准备
 public var current_viewport:Viewport3D;//为实现3D键盘交互准备

 

    然后修改initPapervision函数,把原来默认的场景、相机、视图区更改为这三个类属性:

 

 current_camera = default_camera;
 current_scene = default_scene;
 current_viewport = viewport;

 

    用新的类成员变量重新渲染:

 

 renderer.renderScene(current_scene, current_camera, current_viewport);

 

    2、新建3D键盘交互类:MainKeyboardInteraction

    在这类中我将使用Plane(平面类)加载外部图片贴图来实现3D键盘交互,并且设定使用键盘上的w、a、s、d这4个按键控制Plane的平移,旋转交互效果,代码如下:

 

 package
{
 
 
    import PaperBase;
    import flash.events.KeyboardEvent;
    import org.papervision3d.cameras.FreeCamera3D;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.materials.BitmapFileMaterial;
   
    public class MainKeyboardInteraction extends PaperBase {
      
       public var wdown:Boolean = false;
       public var adown:Boolean = false;
       public var sdown:Boolean = false;
       public var ddown:Boolean = false;
       public var camera:FreeCamera3D;
      
       public function MainKeyboardInteraction() {
         init();
         stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
         stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
       }
      
       public function onKeyDown( event:KeyboardEvent ):void {
         switch(event.keyCode) {//w:87  a:65  s:83  d:68
              case 87:
                  wdown = true;
              break;
              case 65:
                  adown = true;
              break;
              case 83:
                  sdown = true;
              break;
              case 68:
                  ddown = true;
              break;
         }
       }
      
       public function onKeyUp( event:KeyboardEvent ):void {
              switch(event.keyCode) {
              case 87:
                  wdown = false;
              break;
              case 65:
                  adown = false;
              break;
              case 83:
                  sdown = false;
              break;
              case 68:
                  ddown = false;
              break;
         }
       }
      
       override protected function init3d():void {
         camera = new FreeCamera3D(1, 500);
         camera.moveUp(400);
         current_camera = camera;
         for (var x:Number = 0; x <4; x++) {
              for (var y:Number = 0; y <4; y++) {
                  var p:Plane = new Plane(new BitmapFileMaterial("img/concretetex02.jpg"));
                  p.pitch(90);
                  p.x = (x * 1000)-2000;
                  p.z = (y * 1000)-2000;
                  default_scene.addChild(p);
              }
         }
       }
       override protected function processFrame():void {
         if (wdown) {
              camera.moveForward(60);
         }
         if (sdown) {
              camera.moveBackward(60);
         }
         if (adown) {
              camera.yaw( -8);
         }
         if (ddown) {
              camera.yaw( 8);
         }
       }
    }
}

 

    代码思路清晰,无非是用stage来侦听事件(键盘事件)~!最终效果截图如下:

    总结:实现Papervision3D键盘交互效果要注意对舞台的侦听事件的处理,当需要侦听键盘时不要忘了事先import键盘类,并同时override父类相关的函数。

   本教程可自由转载,请注明转载出处,谢谢~!

posted on 2008-11-19 14:51 seal 阅读(412) 评论(0)  编辑  收藏 所属分类: Flex+ActionScript

只有注册用户登录后才能发表评论。


网站导航: