Turn a function into a class which returns an Object3D

Software: Away3D 4.x

Mr Margaret Scratcher, Sr. Member
Posted: 02 August 2012 03:19 PM   Total Posts: 344

Hi there, building external classes is something I’ve wanted to get my head around for a while, so I thought I’d start by trying to make a multi-Material Cube, as it’s something that I find I need quite often, and from a few similar posts it seems that others do too.

I’ve got it working as a function, taking

(height,width,depth,face1Mat,face2Mat,face3Mat,face4Mat,face5mat,face6Mat)

and making an appropriately sized and material’ed cube, but now what I want to do is take that and make it into a class, so I can just do

var multiCube:multiMatCube = new multiMatCube (height,width,depth,face1Mat,face2Mat,face3Mat,face4Mat,face5mat,face6Mat)

and get the same result.

At the moment i have it in the same folder as my base class, and so as I start typing multiMatCube it pops up, and appears in blue, and gives me hinst as I start to fill in the variables, so that bit is working, but what I get when I try to add it to the view is

Error: Implicit coercion of a value of type multiMatCube to an unrelated type away3d.containers:ObjectContainer3D.

How do I get it to return the container that all the planes are in?

Also, can I access the various faces from the bass class by doing

multiCube.face1 ? And say add event listeners?

The other things I’m not sure on is what I need to include - do things like eventlisteners need to be included, or is including them on the base class going to be okay? Same with materials..

Also, currently the class extends sprite, is that right? Does it have to extend anything?

What about namespace arcane - not sure what that even means…

Anyways, here it is:

package
{
 import away3d
.containers.ObjectContainer3D;
 
import away3d.entities.Mesh;
 
import away3d.entities.Entity;
 
import away3d.materials.MaterialBase;
 
import away3d.materials.TextureMaterial;
 
import away3d.textures.BitmapTexture;
 
import away3d.events.LoaderEvent;
 
import away3d.events.MouseEvent3D;

 
import away3d.materials.lightpickers.*;
 
 
import away3d.materials.methods.EnvMapMethod;
 
import away3d.materials.ColorMaterial;
 
import away3d.materials.DefaultMaterialBase;
 
import away3d.materials.BitmapMaterial;
 
import away3d.primitives.PlaneGeometry;


 
import flash.display.Sprite;
 
import flash.display.DisplayObject;
 
import flash.display.StageAlign;
 
import flash.display.StageScaleMode;
 
import flash.display.MovieClip;
 
import flash.display.Loader;
 
import flash.system.LoaderContext;
 
import flash.display.BitmapData;
 
import flash.display.Bitmap;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.events.KeyboardEvent;
 
import flash.events.IOErrorEvent;

 
import flash.net.URLRequest;
 
import flash.net.URLLoader;
 
import flash.net.URLLoaderDataFormat;
 
import flash.geom.Matrix;
 
import flash.system.Security;
 
 
//use namespace arcane;
 

 
 
public class multiMatCube extends Sprite
 {
  
  
var planesContainer:ObjectContainer3D = new ObjectContainer3D;
  var 
face1:Mesh;
  var 
face2:Mesh;
  var 
face3:Mesh;
  var 
face4:Mesh;
  var 
face5:Mesh;
  var 
face6:Mesh;
  
  
  public function 
multiMatCube(height,width,depth,face1Mat,face2Mat,face3Mat,face4Mat,face5mat,face6Mat):void
  {
   
//trace ("creating multicube")
   
var faceGeometry:PlaneGeometry = new PlaneGeometry(111)
   
   
//Top
   
face1 =  new Mesh(faceGeometryface1Mat );
   
face1.scaleX width;
   
face1.scaleZ depth;
   
   
face1.rotationX 0
   face1
.height 2;
   
   
//Bottom
   
face2 =  new Mesh(faceGeometryface2Mat);
   
face2.scaleX width;
   
face2.scaleZ depth;
   
face2.rotationX 180;
   
face2.= -height/2;
   
   
//back
   
face3 = new Mesh (faceGeometryface3Mat);
   
face3.scaleX width;
   
face3.scaleZ height;
   
face3.rotationX 90
   face3
.depth 2;
   
   
//front
   
face4 = new Mesh (faceGeometryface4Mat);
   
face4.scaleX width;
   
face4.scaleZ height;
   
face4.rotationX 270
   face4
.= -depth 2;
   
   
//right
   
face5 = new Mesh (faceGeometryface5mat);
   
face5.scaleX depth;
   
face5.scaleZ height;
   
face5.rotationX = -90
   face5
.rotationY = -90
   face5
.width 2;
   
   
//left
   
face6 = new Mesh (faceGeometryface6Mat);
   
face6.scaleX depth;
   
face6.scaleZ height;
   
face6.rotationX = -90
   face6
.rotationY 90
   face6
.= -width 2;
   
   
planesContainer.addChild(face1);
   
planesContainer.addChild(face2);
   
planesContainer.addChild(face3);
   
planesContainer.addChild(face4);
   
planesContainer.addChild(face5);
   
planesContainer.addChild(face6);
  
  
}
  
  
//public override function get assetType() : String
  //{
   //return AssetType.OBJECTCONTAINER3D;
  //}
  
 
}

(Obviously at this stage there’s loads of includes which most likely don’t need to be there, I’ll start pruning once it’s working…)


Also, this seems like something quite a few people have asked for, is it the kind of thing that if it was coded nicely could be added into away3d? If so, what are the guidelines for making something to be considered for that?

   

Richard Olsson, Administrator
Posted: 02 August 2012 05:08 PM   Total Posts: 1192   [ # 1 ]

Don’t extend Sprite. Extend ObjectContainer3D, and addChild the planes directly to itself instead of to a private planesContainer variable.

However, if you want something like this to be included in Away3D, you should create a Geometry (or PrimitiveBase) sub-class that creates six separate sub-meshes, that can then have individual materials assigned once used in a Mesh.

   

Mr Margaret Scratcher, Sr. Member
Posted: 02 August 2012 05:12 PM   Total Posts: 344   [ # 2 ]

Aha, I see.. I’ll do the interim stage now, and investigate doing it a more ‘proper’ way when time allows.

Thanks!

   

Mr Margaret Scratcher, Sr. Member
Posted: 21 August 2012 02:35 PM   Total Posts: 344   [ # 3 ]

.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X