Unable to add click listener on a cube.

Software: Away3D 4.x

Pradyumn, Newbie
Posted: 27 September 2012 09:32 AM   Total Posts: 3

Hi,

This is my very first project on away3d and the very first post in the forum too. I have setup a view and there’s a cube in it, actually a tile. and I have added a event listener to it.

tile.addEventListener(MouseEvent3D.CLICK,objectClick);

private function objectClick(e:MouseEvent3D) {
    trace(“Anything!”);
}

This I think should do the job, i.e., whenever user clicks on the tile then that function must work, but I’m not able to trace anything in the output window. Have I missed anything in my code.

Help me guys with this please.

——————I’m pasting the code below————————————————

package
{
import flash.display.MovieClip;
import away3d.cameras.Camera3D;
import away3d.containers.View3D;
import flash.geom.Vector3D;
import away3d.controllers.HoverController;
import away3d.controllers.LookAtController;
import away3d.debug.Debug;
import away3d.entities.Mesh;
import away3d.materials.ColorMaterial;
import away3d.primitives.ConeGeometry;
import away3d.primitives.CubeGeometry;
import away3d.primitives.SphereGeometry;
import away3d.events.MouseEvent3D;

// classes for lighting
import away3d.lights.PointLight;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.lights.DirectionalLight;
import away3d.materials.methods.FilteredShadowMapMethod;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

//

import away3d.cameras.Camera3D;
import away3d.containers.ObjectContainer3D;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends Sprite
{
private var cam:Camera3D;
private var lastKey:uint;
private var keyIsDown:Boolean = false;
private var view:View3D;
private var light:DirectionalLight;
private var tile:Mesh;
private var camController:HoverController;
private var desiredTiltAngle:int = 0;
private var inited:Boolean = false;
private var clicked:Boolean = false;
[SWF(width=“800”, height=“600”, frameRate=“24”)]


public function Main()
{
  // create a camera
  initCamera();
 
  // create a view
  initView();
 
  // create light
  initLight();

  // create a tile
  initTile();

  // add a huge surrounding sphere so we really can see what we’re doing
  var largeSphere:SphereGeometry = new SphereGeometry(1500);
  var largeSphereMaterial:ColorMaterial = new ColorMaterial(0xFFFFFF);
  var largeSphereMesh:Mesh = new Mesh(largeSphere, largeSphereMaterial);
  largeSphereMesh.scaleX = -1;
  view.scene.addChild(largeSphereMesh);

  // setup the camera controller
  camController = new HoverController(cam, tile, 180,0);
  camController.distance = 400;
  camController.steps = 1;

  view.addEventListener(MouseEvent.CLICK, flipTileOnClick);
  this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
  this.stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
  this.addEventListener(Event.ENTER_FRAME, update);
}

private function flipTileOnClick(event:MouseEvent):void {
  if(clicked == false) {
  clicked = true;
  view.addEventListener(Event.ENTER_FRAME, moveTileToBack);
  view.removeEventListener(Event.ENTER_FRAME, moveTileToFrot);
  } else if(clicked == true) {
  clicked = false;
  view.addEventListener(Event.ENTER_FRAME, moveTileToFrot);
  view.removeEventListener(Event.ENTER_FRAME, moveTileToBack);
  }
}
 
private function moveTileToBack(e:Event) {
  if(tile.rotationY < 180) {
  tile.rotationY += 14;
  }

}

private function moveTileToFrot(e:Event) {
  if(tile.rotationY > 0) {
  tile.rotationY -= 14;
  }
}

private function initCamera() {
  cam = new Camera3D();
  cam.z = -350;
}

private function initView() {
  stage.quality = “high”;
  view = new View3D(null, cam);
  view.backgroundColor = 0xFFFFFF;
  view.antiAlias = 4;
  addChild(view);
}

private function initLight() {
  light = new DirectionalLight();
  light.position = new Vector3D(30, 100, -700);
  light.lookAt(new Vector3D());
  light.castsShadows = true;
  light.color = 0xFFFFFF;
  light.specular = 0.25;
  light.ambient = .50;
  view.scene.addChild(light);
}

private function initTile() {
  var tileGeo:CubeGeometry = new CubeGeometry(100,100,15);
  var tileMaterial:ColorMaterial = new ColorMaterial(0xb22222);
  tileMaterial.alpha = .9;
  // tell the material to consider the light
  tileMaterial.lightPicker = new StaticLightPicker([light]);
  // tell the material to recieve the shadows
  tileMaterial.shadowMethod = new FilteredShadowMapMethod(light);
  tile = new Mesh(tileGeo, tileMaterial);
  tile.addEventListener(MouseEvent3D.CLICK,objectClick);
  view.scene.addChild(tile);
}

private function objectClick(e:MouseEvent3D) {
  trace(“Anything!”);
}

private function update(e:Event):void
{
  // move the camera responsive to the Mouse X and Y
  moveCamByMouse();
  //rendering the view
  view.render();
}

private function moveCamByMouse() {
  camController.panAngle = 220-(mouseX*0.1);
  camController.tiltAngle = 12.5-(mouseY*0.0416);
}

private function keyDown(e:KeyboardEvent):void
{
  lastKey = e.keyCode;
  keyIsDown = true;
}
private function keyUp(e:KeyboardEvent):void
{
  keyIsDown = false;
}
}
}

   

cyberix3d, Jr. Member
Posted: 02 October 2012 10:31 PM   Total Posts: 34   [ # 1 ]

Try to add:
tile.mouseEnabled = true;

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X