Skip to content

Instantly share code, notes, and snippets.

@creativedrewy
Created November 8, 2012 02:12
Show Gist options
  • Save creativedrewy/4036122 to your computer and use it in GitHub Desktop.
Save creativedrewy/4036122 to your computer and use it in GitHub Desktop.
Use AS3 describeType() to dynamically list out strongly-typed referenced assets in a class
package com.creativedrewy.app.model.assetlibrary
{
import flash.display.Sprite;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
/**
* Base class for asset libraries that index their clips based on the "asset helper"
* model, where assets are ref'd only by class name
*/
public class LinkedAssetLibraryBase
{
private var _previewClipNames:Array;
/**
* Constructor
*/
public function LinkedAssetLibraryBase(linkedAssetHelper:Object)
{
var backgroundVariableXML:XML = describeType(linkedAssetHelper);
_previewClipNames = new Array();
for each (var currentBgNode:XML in backgroundVariableXML..variable) {
var clipClass:String = currentBgNode.@type;
if (determinePreviewClipQualify(clipClass)) {
_previewClipNames.push(clipClass);
}
}
_previewClipNames.sort();
}
/**
* Determine if the string name representation of the current asset qualifies to be in the preview clips
*/
protected function determinePreviewClipQualify(clipClass:String):Boolean {
return true;
}
/**
* The total number of assets in this library
*/
public function get totalAssetCount():int
{
return _previewClipNames.length;
}
/**
* Get a "page" of sprite assets to show based on current page and page size
*/
public function getAssetPageSet(currentPage:int, pageSize:int):Vector.<Sprite>
{
var returnClips:Vector.<Sprite> = new Vector.<Sprite>();
var totalLibraryAssets:int = totalAssetCount;
var startIndex:int = currentPage * pageSize;
var endIndex:int = (currentPage + 1) * pageSize;
if (endIndex > totalLibraryAssets) {
endIndex = totalLibraryAssets;
}
var counter:int = 0;
for each (var clipClass:String in _previewClipNames) {
if (counter >= startIndex && counter < endIndex) {
var AssetClass:Class = getDefinitionByName(clipClass) as Class;
var currentPreview:Sprite = new AssetClass() as Sprite;
currentPreview.name = clipClass;
returnClips.push(currentPreview);
}
counter++;
}
return returnClips;
}
}
}
package com.creativedrewy.app.model.assetlibrary
{
import com.creativedrewy.app.assets.*;
/**
* Helper class for assets generating appropriate references
*/
public class SampleAssetReferences
{
//These clips can be either FXG's in a directory or in a library SWC
public var asset1:SampleAsset1;
public var asset2:SampleAsset2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment