AssetLibrary createIterator how to use?

128 views
Skip to first unread message

Choons

unread,
May 24, 2011, 12:13:56 AM5/24/11
to Away3D.dev
Hi folks- I'm working on my first multi- asset project using the
AssetLibrary. I named everything on the ASSET COMPLETE event and need
to loop through the library to initialize it all. It seems like
createIterator is the function for that but I can't get it to
recognize any of my input params. How do you use that beast?

richardolsson

unread,
May 24, 2011, 4:02:10 AM5/24/11
to Away3D.dev
Hi Choons,

Are you familiar with iterators in general? This is a very simple (but
powerful ;)) implementation of that pattern. This is how you use it if
you want to loop through all assets that has been loaded:

var asset : IAsset;
var it : AssetLibraryIterator = AssetLibrary.createIterator();
while (asset = it.next()) {
trace(asset.name);
}

You can also jump back and forth (or use a for loop if you prefer)
using the numAssets property and setIndex() method, and retrieve the
current asset using the currentAsset property:

var i : uint;
for (i=0; i < it.numAsset; i++) {
it.setIndex(i);
trace(it.currentAsset.name);
}


If you want to limit the iteration to just a particular type, or a
particular namespace, you specify these as parameters in the
createIterator call:

AssetLibrary.createIterator(AssetType.MESH); // Only meshes
AssetLibrary.createIterator(null, "myns"); // All in NS "myns"
AssetLibrary.createIterator(AssetType.MESH, "myns"); // Both

The third example above will only return meshes in namespace "myns",
i.e. no materials, textures or anything else from that namespace, and
no meshes from any other namespace.

If you want to filter using some custom method, use the third
parameter to define a function callback which will be invoked with a
single parameter (the asset) for every asset during the filter stage.
That method must return a boolean indicating whether the particular
asset should be included or not.

function includeIfCar(asset : IAsset) : Boolean
{
if (asset.name.indexOf('car')>=0)
return true;
else
return false;
}

AssetLibrary.createIterator(null,null, includeIfCar);


Regardless of how you invoke createIterator(), the returned object is
always an iterator that you should use the way I described above. Hope
this helps!


Cheers
/R

Ross Smith

unread,
May 24, 2011, 9:24:03 AM5/24/11
to away3...@googlegroups.com
Brilliant feature, thanks for adding this.
--
Ross Smith
evolut...@gmail.com

Choons

unread,
May 24, 2011, 4:31:09 PM5/24/11
to Away3D.dev
OH! I see. I have to instantiate a variable and iterate off that. I
knew it was something simple I was missing. Really loving how I can
organize numerous assets with the library. great implementation
> evolution...@gmail.com
Reply all
Reply to author
Forward
0 new messages