Is it possible to use one loader for multiple sprites?

I have looked at various resources on this topic and it seems to me that I need a Loader for every Sprite that contains an image file (png).

I'm trying to create a Tile render system, and I've created a grid of X sprites Y, but they all actually reference the same image file. Is there any other way to do this? (Make sprite a common png data file)

Sample code 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 );
    }
}

      

0


a source to share


2 answers


I think the answer in your case is to use the Bitmap data contained in the loaded image like this:



var tilesWide:uint = 10;
var tilesHigh:uint = 10;

var tileHolder:Sprite = new Sprite();
this.addChild(tileHolder);

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded);
loader.load(new URLRequest("tile.png"));

function onImgLoaded(e:Event):void
{
    /* Create a template bitmap to hold the image info */
    var templateBitmap:Bitmap = e.target.content;
    var templateBitmapData:BitmapData = templateBitmap.bitmapData;

    /* Loop through your tiles */
    for (var a:uint = 0; a < tilesWide; a++)
    {
        for (var b:uint = 0; b < tilesHigh; b++)
        {
            var tile:Sprite = new Sprite();
            /* Attach the template BitmapData to each tile */
            var tileBitmap:Bitmap = new Bitmap(templateBitmapData);
            tile.addChild(tileBitmap);

            tile.x = a * tile.width;
            tile.y = b * tile.height;

            tileHolder.addChild(tile);
        }
    }
}

      

+2


a source


You can also use SpriteFactory , a small library I wrote specifically for this:

var tilesWide:uint = 10;
var tilesHigh:uint = 10;
var tileHolder:Sprite = new Sprite();
var tilePath:String = "some/image/path.png";

var factory:SpriteFactory = new SpriteFactory();
factory.loadBitmap("tile", tilePath);

for (var a:uint = 0; a < tilesWide; a++)
{
    for (var b:uint = 0; b < tilesHigh; b++)
    {
        var tile:Sprite = factory.newSprite("tile");                        
        tile.x = a * tile.width;
        tile.y = b * tile.height;

        tileHolder.addChild(tile);
    }
}

      



The advantage is that you can use sprites right away and they will be automatically filled with the bitmap after loading it.

0


a source







All Articles