0 Replies Latest reply on Apr 16, 2013 6:04 AM by ThaowMatthew1994

    Where to put removeChild (Away3d 4.x)

    ThaowMatthew1994

      Hey guys i need some help on how to change primitives and removing the previous primitive showing in the scene and also changing materials on the primitive with a click of a button.

       

      Mainly just where to put removeChild.

       

      This is for Away3d

       

       

      package

      {

          import away3d.cameras.*;

          import away3d.containers.*;

          import away3d.controllers.*;

          import away3d.core.base.SubGeometry;

          import away3d.core.base.data.Vertex;

          import away3d.debug.*;

          import away3d.entities.Mesh;

          import away3d.extrusions.*;

          import away3d.filters.*;

          import away3d.lights.*;

          import away3d.materials.*;

          import away3d.materials.lightpickers.*;

          import away3d.materials.methods.*;

          import away3d.primitives.*;

          import away3d.textures.*;

       

          import away3dplus.controllers.SimpleHoverController;

       

          import flash.display.Bitmap;

          import flash.display.BitmapData;

          import flash.display.Sprite;

          import flash.display.StageAlign;

          import flash.display.StageScaleMode;

          import flash.events.Event;

          import flash.events.MouseEvent;

          import flash.geom.Vector3D;

          import flash.text.TextField;

          import flash.text.TextFieldAutoSize;

          import flash.text.TextFormat;

       

          [SWF(backgroundColor="#000000",frameRate="30",quality="LOW")]

       

       

       

       

          public class Main extends Sprite

          {

       

       

       

          //engine variables

          private var view:View3D;

          private var scene:Scene3D;

          // debug

          private var awayStats:AwayStats;

       

          //light objects

          private var pointLight:PointLight;

          private var lightPicker:StaticLightPicker;

       

          //material objects

          private var redColorMaterial:ColorMaterial;

          private var skyBoxCubeTexture:BitmapCubeTexture;

       

          //Buttons   

          private var btnCube:Sprite = new Sprite

          private var btnSphere:Sprite = new Sprite

          private var btnCylinder:Sprite = new Sprite

          private var btnTorus:Sprite = new Sprite   

          private var btnMaterial:Sprite = new Sprite   

          private var btnWireframe:Sprite = new Sprite

       

       

              public function Main() {

                  init();

       

       

          }

       

       

       

                  private function init():void {

                      initEngine();

                      initLights();

                      initCamera();

       

                      initMaterials();

                      initObjects();

       

                      initListeners();

       

       

                      drawButton()

       

                      btnCube.addEventListener(MouseEvent.CLICK, changeCube);

                      btnCube.x = 400;

                      btnCube.y = 25;

                      addChild(btnCube);

       

                      btnSphere.addEventListener(MouseEvent.CLICK, changeSphere);

                      btnSphere.x = 400;

                      btnSphere.y = 60;

                      addChild(btnSphere);

       

                      btnCylinder.addEventListener(MouseEvent.CLICK, changeCylinder);

                      btnCylinder.x = 400;

                      btnCylinder.y = 95;

                      addChild(btnCylinder);

       

                      btnTorus.addEventListener(MouseEvent.CLICK, changeTorus);

                      btnTorus.x = 400;

                      btnTorus.y = 130;

                      addChild(btnTorus);

       

                      //btnMaterial.addEventListener(MouseEvent.CLICK, changeMaterial);

                      btnMaterial.x = 15;

                      btnMaterial.y = 95;

                      addChild(btnMaterial);

       

                      btnWireframe.addEventListener(MouseEvent.CLICK, changeWireframe);

                      btnWireframe.x = 15;

                      btnWireframe.y = 130;

                      addChild(btnWireframe);

       

       

                  }   

       

       

       

       

       

                  /**

                   * Initialise the engine

                   */

                  private function initEngine():void {

                      //stage setup

                      stage.scaleMode = StageScaleMode.NO_SCALE;

                      stage.align = StageAlign.TOP_LEFT;

       

                      // 3d view - window into 3d scene

                      view = new View3D();

                      addChild(view);

       

                      // 3d scene.

                      scene = view.scene;

       

                      // stats

                      awayStats = new AwayStats(view);

                      addChild(awayStats);

                  }

       

                  /**

                   * Initialise the lights

                   */

                  private function initLights():void {

                      //point light

                      pointLight = new PointLight();

                      scene.addChild(pointLight);

                      lightPicker = new StaticLightPicker([pointLight]);

                  }

       

                  private function initCamera():void {

                      var hoverCameraManager:SimpleHoverController = new SimpleHoverController(view, 700, [pointLight]);

                  }

       

       

       

                  /**

                   * Initialise Buttons

                   */

       

       

                  private function drawButton():void {

                      var textLabel:TextField = new TextField()

                      btnCube.graphics.clear();

                      btnCube.graphics.beginFill(0xFFFFFF); // white

                      btnCube.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH

       

                      textLabel.text = "CUBE";

                      textLabel.x = 22;

                      textLabel.y = 4;

                      textLabel.selectable = false;

                      btnCube.addChild(textLabel)

       

       

       

       

       

       

       

                  var textLabel01:TextField = new TextField()   

       

                  btnSphere.graphics.clear();

                  btnSphere.graphics.beginFill(0xFFFFFF); // white

                  btnSphere.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH   

       

                  textLabel01.text = "SPHERE";

                  textLabel01.x = 18;

                  textLabel01.y = 3;

                  textLabel01.selectable = false;

                  btnSphere.addChild(textLabel01)

       

       

       

       

                  var textLabel03:TextField = new TextField()   

       

                  btnCylinder.graphics.clear();

                  btnCylinder.graphics.beginFill(0xFFFFFF); // white

                  btnCylinder.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH   

       

                  textLabel03.text = "CYLINDER";

                  textLabel03.x = 10;

                  textLabel03.y = 3;

                  textLabel03.selectable = false;

                  btnCylinder.addChild(textLabel03)   

       

       

                  var textLabel04:TextField = new TextField()   

       

                  btnTorus.graphics.clear();

                  btnTorus.graphics.beginFill(0xFFFFFF); // white

                  btnTorus.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH   

       

                  textLabel04.text = "TORUS";

                  textLabel04.x = 17;

                  textLabel04.y = 3;

                  textLabel04.selectable = false;

                  btnTorus.addChild(textLabel04)   

       

       

       

       

                  var textLabel05:TextField = new TextField()   

       

                  btnMaterial.graphics.clear();

                  btnMaterial.graphics.beginFill(0xFFFFFF); // white

                  btnMaterial.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH   

       

                  textLabel05.text = "MATERIAL";

                  textLabel05.x = 6;

                  textLabel05.y = 3;

                  textLabel05.selectable = false;

                  btnMaterial.addChild(textLabel05)       

       

       

                  var textLabel06:TextField = new TextField()   

       

                  btnWireframe.graphics.clear();

                  btnWireframe.graphics.beginFill(0xFFFFFF); // white

                  btnWireframe.graphics.drawRoundRect(0, 0, 80, 25, 10, 10); // x, y, width, height, ellipseW, ellipseH   

       

                  textLabel06.text = "WIREFRAME";

                  textLabel06.x = 2;

                  textLabel06.y = 3;

                  textLabel06.selectable = false;

                  btnWireframe.addChild(textLabel06)       

       

       

       

       

                  }

       

       

       

       

                  /**

                   * Initialise the materials

                   */

                  private function initMaterials():void {

                      // red color

                      redColorMaterial = new ColorMaterial(0xFF0000, 0.8);

                      redColorMaterial.lightPicker = lightPicker;

       

                      //

                      skyBoxCubeTexture = new BitmapCubeTexture(new EnvPosX().bitmapData, new EnvNegX().bitmapData, new EnvPosY().bitmapData, new EnvNegY().bitmapData, new EnvPosZ().bitmapData, new EnvNegZ().bitmapData);

       

                  }

       

                  /**

                   * Initialise the scene objects

                   */

                  private function initObjects():void {

       

                      var trident:Trident = new Trident(100);

                      trident.x = 0;

                      trident.y = 0;

                      scene.addChild(trident);

       

                      //Geometry - Geometry is a collection of SubGeometries, each of which contain the actual geometrical data such as vertices, normals, uvs, etc.

                      {

                          ////PrimitiveBase    - PrimitiveBase is an abstract base class for mesh primitives, which are prebuilt simple meshes.

                          {

                              //CubeGeometry     A Cube primitive mesh.

                              var newCubeGeometry:CubeGeometry = new CubeGeometry(200, 200, 200, 50, 50, 50, true);

       

                              //SphereGeometry - A UV Sphere primitive mesh.

                              var newSphereGeometry:SphereGeometry = new SphereGeometry(50, 16, 12, true);

       

                              //CapsuleGeometry     A UV Capsule primitive mesh.

                              var newCapsuleGeometry:CapsuleGeometry = new CapsuleGeometry(50, 100, 16, 12, true);

       

                              //ConeGeometry     A UV Cone primitive mesh.

                              //var newConeGeometry:ConeGeometry = new ConeGeometry(50, 100, 16, 1, true, true);

       

                              //CylinderGeometry     A UV Cylinder primitive mesh.

                              var newCylinderGeometry:CylinderGeometry = new CylinderGeometry(50, 50, 100, 16, 1, true, true);

       

                              //PlaneGeometry     A Plane primitive mesh.

                              var newPlaneGeometry:PlaneGeometry = new PlaneGeometry(100, 100, 1, 1, true);

       

                              //RegularPolygonGeometry     A UV RegularPolygon primitive mesh.

                              var newRegularPoligonGeometry:RegularPolygonGeometry = new RegularPolygonGeometry(100, 16, true);

       

                              //TorusGeometry     A UV Torus primitive mesh.

                              var newTorusGeomentry:TorusGeometry = new TorusGeometry(50, 50, 15, 8, true);

                          }

                      }

       

                      // Entity - The Entity class provides an abstract base class for all scene graph objects that are considered having a "presence" in the scene,

                      //            in the sense that it can be considered an actual object with a position and a size (even if infinite or idealised), rather than a grouping.

       

       

                      {   

                          //Mesh - Mesh agregates instance of a Geometry, augmenting it with a presence in the scene graph, a material, and an animations tate.

                          //            It consists out of SubMeshes, which in turn correspond to SubGeometries. SubMeshes allow different parts of the geometry to be assigned different materials.

       

                          //var cube:Mesh = new Mesh(newCubeGeometry, redColorMaterial);

                          //cube.x = 100;

                          //cube.y = 100;   

                          //cube.z = 100;

                          //scene.addChild(cube);

       

       

                          //var sphere:Mesh = new Mesh(newSphereGeometry, redColorMaterial);

                          //sphere.x = -75;

                          //sphere.y = -150;

                          //scene.addChild(sphere);

       

                          //var capsule:Mesh = new Mesh(newCapsuleGeometry, redColorMaterial);

                          //capsule.x = -200;

                          //capsule.y = -150

                          //scene.addChild(capsule);

       

                          //var cone:Mesh = new Mesh(newConeGeometry, redColorMaterial);

                          //cone.x = -200;

                          //cone.y = 150;

                          //cone.showBounds = true;

                          //scene.addChild(cone);

       

                          //var cylinder:Mesh = new Mesh(newCylinderGeometry, redColorMaterial);

                          //cylinder.x = -75;

                          //cylinder.y = 150;

                          //scene.addChild(cylinder);

       

                          //var plane:Mesh = new Mesh(newPlaneGeometry, redColorMaterial);

                          //plane.x = -75;

                          //plane.y = 275;

                          //scene.addChild(plane);

       

                          //var poligon:Mesh = new Mesh(newRegularPoligonGeometry, redColorMaterial);

                          //poligon.x = -275;

                          //poligon.y = 275;

                          //scene.addChild(poligon);

       

                          //var torus:Mesh = new Mesh(newTorusGeomentry, redColorMaterial);

                          //torus.x = -275;           

                          //scene.addChild(torus);

       

                          //SkyBox     A SkyBox class is used to render a sky in the scene.

                          //var skyBox:SkyBox = new SkyBox(skyBoxCubeTexture);

                          //scene.addChild(skyBox);

       

                          // SegmentSet

                          {

                              //WireframeAxesGrid - Class WireframeAxesGrid generates a grid of lines on a given planeWireframeAxesGrid

                              //var wireFrameAxesGrid:WireframeAxesGrid = new WireframeAxesGrid(4, 400, 1);

                              //scene.addChild(wireFrameAxesGrid);

       

                              //WireframeGrid     Class WireframeGrid generates a grid of lines on a given planeWireframeGrid

                              //var wireframeGrid:WireframeGrid = new WireframeGrid(10, 100, 5, 0x0000FF);

                              //wireframeGrid.x = 75;

                              //wireframeGrid.y = 275;

                              //scene.addChild(wireframeGrid);

       

                              //WireframePrimitiveBase

                              {

                                  //WireframeCube    - Class WireFrameGrid generates a grid of lines on a given planeWireFrameGrid

                                  //var wireFrameCube:WireframeCube = new WireframeCube(100, 100, 100, 0x0000FF, 5);

                                  //wireFrameCube.x = 75;

                                  //scene.addChild(wireFrameCube);

       

                                  //WireframeSphere - Class WireFrameGrid generates a grid of lines on a given planeWireFrameGrid

                                  //var wireFrameSphere:WireframeSphere = new WireframeSphere(50, 16, 12, 0x0000FF, 5);

                                  //wireFrameSphere.x = 75;

                                  //wireFrameSphere.y = -150;

                                  //scene.addChild(wireFrameSphere);

       

                                  //WireframePlane

                                  //var wireframePlane:WireframePlane = new WireframePlane(100, 100, 10, 10, 0x0000FF, 5);

                                  //wireframePlane.x = 175;

                                  //wireframePlane.y = 275;

                                  //scene.addChild(wireframePlane);

       

                              }

                          }

                      }

                  }

       

       

       

       

                  /**

                   * Initialise the listeners

                   */

                  private function initListeners():void {

                      addEventListener(Event.ENTER_FRAME, onEnterFrame);

                  }

       

                  /**

                   * render loop

                   */

                  private function onEnterFrame(event:Event):void {

       

       

                      view.render();

                  }

                  /**

                   * Intialise the buttons events

                   */

                  private function changeCube(event:MouseEvent):void {

       

                      var newCubeGeometry:CubeGeometry = new CubeGeometry(200, 200, 200, 50, 50, 50, true);

                      var cube:Mesh = new Mesh(newCubeGeometry, redColorMaterial);

                      cube.x = 100;

                      cube.y = 100;   

                      cube.z = 100;

                      scene.addChild(cube);

       

       

       

       

       

                  }

       

                      private function changeSphere(event:MouseEvent):void {   

       

                           var newSphereGeometry:SphereGeometry = new SphereGeometry(50, 16, 12, true);

                           var sphere:Mesh = new Mesh(newSphereGeometry, redColorMaterial);

                           scene.addChild(sphere);

       

       

       

                      }

       

                      private function changeCylinder(event:MouseEvent):void {   

       

                          var newCylinderGeometry:CylinderGeometry = new CylinderGeometry(50, 50, 100, 16, 1, true, true);

                          var cylinder:Mesh = new Mesh(newCylinderGeometry, redColorMaterial);

                          scene.addChild(cylinder);

       

                      }

       

                      private function changeTorus(event:MouseEvent):void {   

       

                          var newTorusGeomentry:TorusGeometry = new TorusGeometry(50, 50, 15, 8, true);

                          var torus:Mesh = new Mesh(newTorusGeomentry, redColorMaterial);

                          scene.addChild(torus);

       

                      }

       

                      private function changeWireframe(event:MouseEvent):void {   

       

                          var wireFrameCube:WireframeCube = new WireframeCube(100, 100, 100, 0x0000FF, 5);

       

                          scene.addChild(wireFrameCube);

       

       

       

       

       

       

              }

          }

       

      }

       

       

       

       

       

       

       

       

      Thanks, Matthew