Single Loader to multiple Sprite Possible?

Posted by Jeffrey Chee on Stack Overflow See other posts from Stack Overflow or by Jeffrey Chee
Published on 2009-05-06T06:59:10Z Indexed on 2010/04/06 22:03 UTC
Read the original article Hit count: 176

Filed under:

I've looked in various resources regarding this topic, and it seems to me that I need a Loader for every Sprite which contains an image file (png).

I'm trying to make a Tile Rendering System, and have created a grid of X by Y sprites, but all of them actually reference the same image file. Is there any other way to do this? (Make the sprite share the same png data file)

Some sample code of what I have done.

// Create an array of X * Y Loaders
var cTileLoaders:Array = new Array( 100 ); // for example 10 by 10 grid
var cTiles:Array = new Array( 100 );
var nIndex:int = 0;
var nImgLoadCount:int = 0;
for ( ; 100 > nIndex; ++nIndex ) {
    cTileLoaders[ nIndex ] = new Loader();
    cTiles[ nIndex ] = new Sprite();
    // perform more sprite initialization
    ....
    cTileLoaders[ nIndex ].contentLoaderInfo.addEventListener( Event.COMPLETE, ImageLoaded
    cTileLoaders[ nIndex ].Load( new URLRequest( "some image path" ) );
}
// handler for image loaded
function ImageLoaded( eEvent:Event ):void {
    ++nImgLoadCount;
    // when all 100 sprite data are loaded
    // assuming there is no i/o error
    if ( 100 == nImgLoadCount ) { 
        cTiles[ nIndex ].addChild( cTileLoaders[ nIndex ].content );
    }
}

© Stack Overflow or respective owner

Related posts about actionscript-3