I want to post series of articles about tool I am building during free time.
All started then I got this insane idea of building GUI library, for creating GUI in applications more fluently and conversant then FLEX. (there is so much things I hate about FLEX…)
I needed a asset library to handle my assets easily. I starter with something simple, but ended up having a lot of features I initially didn’t planed.
Read all features here : http://code.google.com/p/msa-lib/
I will write couple of posts about this library, and transform them to documentation.
If it’s something that interests you, grab code in http://msa-lib.googlecode.com/svn/trunk/ and lets get started.
Then you build application, you almost always have some sort of assets: texts, images, sounds, binary files for 3d and so on.
I am dividing those assets in 3 category:
- Internaly embedded assets.
- External permanent asset. (it’s loaded once, usually in the beginning, and then never removed from memory. )
- External temporal asset. (it’s loaded then needed, and unloaded then not needed any-more.)
Then you deal with internally embedded assets, there is not much to do with them, you just use it then you need it. With external assets its a bit trickier, that’s there AssetLibrary comes in.
Lets just start with most simple example possible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| package examples {
import com.mindscriptact.assetLibrary.AssetLibrary;
import com.mindscriptact.assetLibrary.AssetLibraryIndex;
import flash.display.Sprite;
public class MinimalLoad extends Sprite {
public function MinimalLoad(){
// add assets to library.
var assetIndex:AssetLibraryIndex = AssetLibrary.getIndex();
assetIndex.addFileDefinition("test1", "assets/simpleTest/test1.swf");
// load asset and send it to function.
AssetLibrary.sendAssetToFunction("test1", handleTest1);
}
private function handleTest1(asset:SWFAsset):void {
// get instance of object linked in asset library.
var testSprite:Sprite = asset.getSprite("SquareA_SPR");
this.addChild(testSprite);
testSprite.x = 100;
testSprite.y = 100;
}
}
} |
To use external assets you need to define them first. AssetLibraryIndex class is created for that.
To get it’s instance you need to call AssetLibrary.getIndex() function. In most cases you will be defining you assets once, and then application starts. After assets are defined you most likely will never use this class again. AssetLibraryIndex class lets you define folders, groups, add file definitions from xml file, and add single file definitions, lets start with those.
Every file definition in AssetLibrary needs an Id. This id is used to work with asset instead of all it’s de-tales like url, asset type, how it’s loaded and such. Most simple asset has id and url. To add asset we use assetIndex.addFileDefinition(); function.
Now then asset is defined we can start using it. We tell AssetLibrary to load it and send asset as parameter to function. We use AssetLibrary.sendAssetToFunction() for that. This function expects assedId and function to send asset to.
Then we write asset handling function we must remember, that same as event handling function gets an event object as parameter, asset handler gets asset as parameter. Because we are using swf asset, we will get SWFAsset class object. This object can be used to get any instance of symbol in assets library. We need to know the type of linked object and its ActionScript linkage class.
Next time I explain how permanent and temporal assets differ, and how to track loading progress with this tool.