addChild away3d element in another class

Software: Away3D 4.x

Goupix, Newbie
Posted: 14 March 2012 01:15 AM   Total Posts: 9

Hi,

Here is my problem:
I have 3 class: Main.as, Class3dEngine.as, Class3dLight.as.
Main () calls the two other class.
Class3dEngine () generates the scene, the camera view and
Class3dLight () generates the lights

I want to insert the light in my scene.
In Class3dLight () I wanted to make a scene.addChild (light) but I can not do it.

Here are my files
Main.as

package 
{
 import flash
.display.*;
 
import flash.events.Event;
 public class 
Main extends Sprite 
 {
  
  
public function Main():void 
  {
   
if (stageinit();
   else 
addEventListener(Event.ADDED_TO_STAGEinit);
  
}
  
  
private function init(e:Event null):void 
  {
   removeEventListener
(Event.ADDED_TO_STAGEinit);
   
// entry point
   
   //Engine
   
var InClass3dEngine Class3dEngine = new Class3dEngine();
    
addChild(InClass3dEngine);
   
//Light
   
var InClass3dLight Class3dLight = new Class3dLight();
    
addChild(InClass3dLight);
  
}
 }

Class3dEngine.as

package  
{
 import flash
.display.*;
 
import flash.events.*;
 
import away3d.library.*;
 
import away3d.library.assets.*;
 
import away3d.cameras.Camera3D;
 
import away3d.containers.Scene3D;
 
import away3d.containers.View3D;
 
import away3d.controllers.HoverController;
 
import away3d.loaders.parsers.*;
 
import away3d.debug.*;

 public class 
Class3dEngine extends Sprite 
 {
  
//engine variables
  
public var scene:Scene3D;
  public var 
camera:Camera3D;
  public var 
view:View3D;
  public var 
cameraController:HoverController;
  
  public function 
Class3dEngine() 
  
{
   
//Base
   
Parsers.enableAllBundled();
   
   
//Creation d'une nouvelle scene
   
scene = new Scene3D();
   
   
//Initialisation de la camera
   
camera = new Camera3D();
   
camera.lens.far 2100;
   
   
//Initialisation de la vue
   
view = new View3D();
   
view.scene scene;
   
view.camera camera;
   
   
//setup controller to be used on the camera
   
cameraController = new HoverController(cameranull4520100010);
   
   
//on affiche la vue
   
addChild(view);
   
   
//Affichage des stats
   
addChild(new AwayStats(view));
  
}
 }

Class3dLight.as

package  
{
 import flash
.display.Sprite;
 
import flash.geom.Vector3D;
 
import away3d.lights.DirectionalLight;
 
import away3d.materials.lightpickers.StaticLightPicker;
 
 public class 
Class3dLight extends Sprite
 {
  
public var light:DirectionalLight;
  public var 
lightPicker:StaticLightPicker;
  public var 
direction:Vector3D;
  
  public function 
Class3dLight() 
  
{
   light 
= new DirectionalLight(-1, -11);
   
direction = new Vector3D(-1, -11);
   
lightPicker = new StaticLightPicker([light]);
   
addChild(light);
  
}
 }

Thank you,
Kevin

   

Fab4ce, Jr. Member
Posted: 14 March 2012 09:59 AM   Total Posts: 44   [ # 1 ]

Hi,

instead of adding the light, you simply apply the light to a material

material.lightPicker = your_lightPicker;

   

Goupix, Newbie
Posted: 14 March 2012 12:06 PM   Total Posts: 9   [ # 2 ]

But in case I want to apply a single light source for my whole scene and then all my objects. example: the sun.

The usefulness of the system is able to generate just the lights and you can modify the display by passing parameters.
One class but several results.

I will also apply to principle by creating a class to generate the objects by like this: myclass(nameofmyfile).

I do not know if I am very clear and I am sorry.

   

Avatar
Fabrice Closier, Administrator
Posted: 14 March 2012 12:22 PM   Total Posts: 1265   [ # 3 ]

LightHelper might help you here.

   

Goupix, Newbie
Posted: 14 March 2012 01:05 PM   Total Posts: 9   [ # 4 ]

Thank you for the tip, gonna make my life easier.

But for what I wanted to do, there is no solution? because I guess I’ll get the same error when I want my mesh insert

   

Fab4ce, Jr. Member
Posted: 14 March 2012 01:36 PM   Total Posts: 44   [ # 5 ]

meshes are added to the scene like you said:

scene.addChild(mesh);

if you need to add 3d stuff to your class, i advise a setup like this:

import away3d.containers.ObjectContainer3D;
public class Class3dStuff extends ObjectContainer3D

...
addChild(mesh);
...
}

later you add the instance of the Class3dStuff to your scene:

var class3dStuff = new Class3dStuff ();
scene.addChild(class3dStuff );

 

   

Goupix, Newbie
Posted: 15 March 2012 10:04 PM   Total Posts: 9   [ # 6 ]

I just changed my code and it’s perfect.
Thank you very much, it’s been a pleasure to see an active forum community.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X