Saturday, 17 November 2012

Check Cube face click in Away3D

I was trying to figure out how to check which side of the cube was being clicked in Away3D 3.6. I have a Cube primitive with a different colour material on each face of the Cube. The mouse down event can be added to the cube, but not to each individual face. I looked to Google and the Away3D forum for a solution and found you can use the Cube’s material property to check which face is being clicked on. Here is my code.

package 
{
 import away3d.events.MouseEvent3D;
 import away3d.materials.ColorMaterial;
 import away3d.primitives.Cube;
 import away3d.containers.View3D;
 import away3d.cameras.Camera3D;
 import away3d.containers.Scene3D;
 import away3d.primitives.data.CubeMaterialsData;
 
 import flash.display.Sprite;
 import flash.events.Event;

 [SWF(backgroundColor="#ffffff", frameRate="60", quality="HIGH", width="800", height="600")]
 
 public class Main extends Sprite
 {
  
  private var scene:Scene3D;
  private var camera:Camera3D;
  private var view:View3D;
  
  private var cube:Cube;
  private var mat1:ColorMaterial; 
  private var mat2:ColorMaterial;
  private var mat3:ColorMaterial;
  private var mat4:ColorMaterial;
  private var mat5:ColorMaterial;
  private var mat6:ColorMaterial;
 

  public function Main()
  {
   createView();
   createScene();
  }

 
  private function createView():void
  {
   //setup away3D
   scene = new Scene3D();
   
   camera = new Camera3D();
   camera.z = -1000;
   
   view = new View3D();
   view.scene = scene;
   view.camera = camera;
   view.x = stage.stageWidth / 2;
   view.y = stage.stageHeight / 2;
   addChild(view);
   
   
   //initialise enter frame listener
   addEventListener(Event.ENTER_FRAME, ef);
  }

 
    
  private function createScene():void
  {
    //colours materials
    mat1 = new ColorMaterial(0x0000ff);
    mat2 = new ColorMaterial(0x00ff00);
    mat3 = new ColorMaterial(0xff0000);
    mat4 = new ColorMaterial(0xFFCC00);
    mat5 = new ColorMaterial(0x0066FF);
    mat6 = new ColorMaterial(0x660033);
      

   //define cube materials
   var cubeMats:CubeMaterialsData = new CubeMaterialsData();
   cubeMats.front = mat1;
   cubeMats.bottom = mat2;
   cubeMats.back = mat3;  
   cubeMats.top = mat4;
   cubeMats.right = mat5;
   cubeMats.left = mat6;
 
   
   //add cube
   cube = new Cube();
   cube.cubeMaterials = cubeMats;
   cube.width = 100,
   cube.height = 100,
   cube.depth = 100,
   cube.addOnMouseDown(mDown);
   scene.addChild(cube);
  }


  private function mDown(e:MouseEvent3D):void 
  {
   switch(e.material)
   {
    case mat1:
     trace('material 1');
    break;
    
    case mat2:
     trace('material 2');
    break;
    
    case mat3:
     trace('material 3');
    break;
    
    case mat4:
     trace('material 4');
    break;
    
    case mat5:
     trace('material 5');
    break;
    
    case mat6:
     trace('material 6');
    break;
   }
  }

 
  //rendering scene
  private function ef(e:Event):void
  {
   cube.rotationY += 0.5;
   cube.rotationX += 0.5;
   
   view.render();
  }
  
 }
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP