开发者

Loading and unloading new models

My apologies if this question has been posted before, I am simply unable to source out the solution to my little problem.


I am using pv3d to load a couple of models for a school project. The models typically don't do much, they just spin on a dial when fired up.

At any given time, there are only two models shown. My initial thought is to utilize two global Collada instances and have a function call the Collada load function to load the new models into the instances. Looking through the Collada parser, it would seem that the load function appends the new model, leaving the existing models up there, as opposed to loading in a new set of vertices.

Fair enough. At this point I decide that I should remove the models from the scene, and c开发者_运维问答reate new ones every time the function fires.

Here is where my problem lies. This probably from my lack of understanding on the workings of AS3/pv3d, so please bear with me. When I remove the models from the scene and add them again, the models do not appear within the scene. However, the model instances still remain traceable when I run a trace.

Here is the code for reference. I omitted the portions which are duplicates.

Instances are created on a global level as onRenderTick can't seem to reference it if I create it on any other level

public var model:Collada = new Collada();
public var model2:Collada = new Collada();

The initial properties such as x y position and pitch are set when the dials are created

public function setupDials():void {
   var materialList:MaterialsList = new MaterialsList();
   var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial("assets/images/UV/marble.jpg");
   materialList.addMaterial(bitmapFileMaterial, "all");
   dial = new Collada("assets/Dial.dae", materialList);
   dial2 = new Collada("assets/Dial.dae", materialList);
   dial.scale = 2;
   dial.x = 400;
   dial.y = -100;
   dial.pitch(-10);
   dial2.scale = 2;
   dial2.x = -400;
   dial2.y = -100;
   dial2.pitch(-10);
   scene.addChild(dial);   
   scene.addChild(dial2);

   // run once only
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10);
  }

After the dials are setup, the models are loaded using loadAnimals()

public function loadAnimals(param1:String):void {

   if (!first) {
    scene.removeChild(model);
    scene.removeChild(model2);
    initNewModels();
   } // end if

   first = false;
   model.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   model2.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);   

   if (param1 == "environment1") {
    var leopardMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[0]);
    leopardMats.addMaterial(bitmapFileMaterial, "all");
    model.load("assets/Leopard.dae", leopardMats);
    model.scale = 2;

    var wolverineMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial2:BitmapFileMaterial = new BitmapFileMaterial(textures[1]);
    wolverineMats.addMaterial(bitmapFileMaterial2, "all");
    model2.load("assets/Wolverine.dae", wolverineMats);
    model2.scale = 0.7;
   }

   else if (param1 == "environment2") {
    var markhorMats:MaterialsList = new MaterialsList();
    var markhorFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[4]);
    markhorMats.addMaterial(markhorFileMaterial, "all");
    model.load("assets/Markhor.dae", markhorMats);
    model.scale = 2;

    var oryxMats:MaterialsList = new MaterialsList();
    var oryxFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[5]);
    oryxMats.addMaterial(oryxFileMaterial, "all");
    model2.load("assets/Oryx.dae", oryxMats);
    model2.scale = 10;
   }
  }

_adds the loaded daes onto the scene, self explanatory

  public function daeLoaded(e:FileLoadEvent):void {
   e.target.removeEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   scene.addChild(DisplayObject3D(e.target));
  }

initNewModels() is called upon removal of the models from the scene, to add new models

  public function initNewModels():void {
   var model:Collada = new Collada();
   var model2:Collada = new Collada();
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10); 
  } // end initModels function

At this juncture, using this code, what happens is that the models initially load fine on first click. When I try to load subsequent models, the spinning dials remain, but the models are nowhere to be seen. However, they are traceable when I run a trace, returning me their x, y, z coordinates.

My questions are:

a) Is there another way to completely remove the model from the scene? The item should not be traceable when I call for scene.removeChild(model), but yet it is.

b) Is there a better way to approach this? I probably should not be creating the new models from the initNewModels function, as it seems to me that doing it this way would cause the other functions to be unable to render the new models (hence causing it to be not visible).

I would have preferred to try and figure this out on my own as it seems to be a rather simple issue, but my deadline is approaching and I should not dwell on something too long. I appreciate any help rendered. Much thanks for looking this through!


In the initNewModels() function you are re-declaring model and model2 variables so you are not working with the global-scope Collada instances but with new function-scope variables.

For what you are doing you should really give a look to Object Oriented Programming in AS3 because you are duplicating a lot of code. You should be able to be more prolific and avoid these kind of errors with some simple OOP and maybe an XML file (or an array for configuration)!

Hope it helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜