AssetLoader dispatches events to wrong target

Software: Away3D 4.x

volzh, Newbie
Posted: 11 August 2011 06:09 AM   Total Posts: 4

I used AssetLoader to load mesh and animation data in sequence like below.

1. create new AssetLoader and listen AssetComplete event.
2. after loading mesh data completes, make mesh
3. make new AssetLoader instance to load animation data, and listen AssetComplete

then after animation data loaded, the first instance of AssetLoader catches AssetComplete event that should be dispatched second instance of AssetLoader.
So, it causes error at onMeshLoaded method.

Did i do something wrong?

class Model3DCache extends EventDispatcher
{
 
private var _modelURL:String;
 private var 
_textureURL:String;
 private var 
_animationURL:String;
 
 private var 
_mesh:Mesh;
 private var 
_controller:SmoothSkeletonAnimator;
 
 public function 
Model3DCache(modelURL:StringtextureURL:StringanimationURL:String null)
 
{
  this
._modelURL modelURL;
  
this._textureURL textureURL;
  
this._animationURL animationURL;
  
  
// create first instance of AssetLoader, loading Mesh here.
  
var ldr:AssetLoader = new AssetLoader(); 
  
ldr.load(new URLRequest(this._modelURL));
  
ldr.addEventListener(AssetEvent.ASSET_COMPLETEonMeshLoaded);
 
}
 
 
protected function onMeshLoaded(event:AssetEvent):void
 {
  trace
('onMeshLoaded');

  
this._mesh event.asset as Mesh;

  
this.setMaterial();
  
this.loadAnimation();
 
}
 
 
private function setMaterial():void
 {
  
var mat:BitmapFileMaterial = new BitmapFileMaterial(this._textureURL);
  
this._mesh.material mat;
 

 
 
private function loadAnimation():void
 {
  
// create second instance of AssetLoader, loading Animation here.
  
var ldr:AssetLoader = new AssetLoader();
  
ldr.load(new URLRequest(this._animationURL));
  
ldr.addEventListener(AssetEvent.ASSET_COMPLETEonAnimationLoaded);
 
}
 
 
protected function onAnimationLoaded(event:AssetEvent):void
 {
  
var seq:SkeletonAnimationSequence event.asset as SkeletonAnimationSequence;
  
seq.name event.asset.assetNamespace;
  
  
_controller = new SmoothSkeletonAnimator(SkeletonAnimationState(this._mesh.animationState));
  
_controller.addSequence(seq);
  
_controller.play(seq.name);
 
}
 
   

Richard Olsson, Administrator
Posted: 11 August 2011 07:20 AM   Total Posts: 1192   [ # 1 ]

This sounds very strange. I can see that you are listening for ASSET_COMPLETE. Are you sure that there is only a single asset in the mesh file? Otherwise that would explain why the ASSET_COMPLETE fires several times. If the file contains materials (likely) or anything else, but you only care about the mesh, I would suggest listening for MESH_COMPLETE instead, and to remove the listener in onMeshLoaded.

For the second load, according to the same principle, I would listen for ANIMATION_COMPLETE rather than ASSET_COMPLETE.

If this doesn’t solve your problem, send me the files and I’ll see if this is actually a bug in the framework. But I doubt that since two separate instances of AssetLoader have nothing to do with each other.

   

volzh, Newbie
Posted: 11 August 2011 10:40 AM   Total Posts: 4   [ # 2 ]

onMeshLoaded is called twice because of AssetEvent.MESH_COMPLETE and AssetEvent.SKELETON_COMPLETE.

so. when skeleton event is dispatched, it causes error.

No strange things happen as your adviece.

I really appriciate your answer and advice.
It’s absolutely helpful.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X