转动的平面,鼠标点击后反方向转动
package
{
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.events.MouseEvent3D;
import away3d.materials.TextureMaterial;
import away3d.primitives.PlaneGeometry;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.display.*;
import away3d.utils.Cast;
/**
* ...
* @author yl
*/
[SWF(width='800',height='600',frameRate="60", backgroundColor="0x000000")]
public class MyAway3d_1 extends Sprite
{
//floor的贴图图片
[Embed(source = "../embeds/myAway3d_1.jpg")]
private static var FloorMaterial:Class;
//创建视口
private var _view:View3D;
//创建平面几何对象
private var _planeGeometry:PlaneGeometry;
//创建平面几何对象的容器
private var _planeMesh:Mesh;
//控制旋转方向的变量
private var _direction:Boolean;
public function MyAway3d_1()
{
//侦听舞台初始化完毕
if (stage) {
init();
}else {
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(e:Event=null):void {
//trace("舞台初始化完成!");
//设置舞台缩放模式和对齐方式
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
this.removeEventListener(Event.ADDED_TO_STAGE, init);
//实例化视口
_view = new View3D();
addChild(_view);
//设置抗锯齿参数
_view.antiAlias = 2;
//实例化一个长宽都是300的平面对象
_planeGeometry = new PlaneGeometry(300, 300);
//实例化平面几何对象的容器,第一个参数是平面几何对象,第二个参数是贴图数据
_planeMesh = new Mesh(_planeGeometry, new TextureMaterial(Cast.bitmapTexture(FloorMaterial)));
//设置容器可交互
_planeMesh.mouseEnabled = true;
//容器侦听鼠标点击事件
_planeMesh.addEventListener(MouseEvent3D.CLICK, clickHandler);
//将容器添加到视口的场景中
_view.scene.addChild(_planeMesh);
//设置摄像机的位置
_view.camera.z = -400;
_view.camera.y = 260; //可以把这个值改成1试试看,这样可以有更加直观的感受
//_view.camera.x = 90;
//设置摄像机始终指向平面
_view.camera.lookAt(_planeMesh.position);
//_view.camera.lookAt(new Vector3D());
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(Event.RESIZE, onResize);
onResize();
}
private function clickHandler(e:MouseEvent3D):void {
//鼠标点击变换运动方向
_direction = !_direction;
}
private function onResize(e:Event=null):void {
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
private function onEnterFrame(e:Event):void {
if (!_direction) {
_planeMesh.rotationY += 1;
}else {
_planeMesh.rotationY -= 1;
}
_view.render();
}
}
}