Domanda

Ho cercato varie risorse su questo argomento e mi sembra che abbia bisogno di un caricatore per ogni Sprite che contiene un file di immagine (png).

Sto cercando di creare un sistema di rendering per piastrelle e ho creato una griglia di sprite X per Y, ma tutti fanno effettivamente riferimento allo stesso file di immagine. C'è un altro modo di fare questo? (Fai in modo che lo sprite condivida lo stesso file di dati png)

Qualche codice di esempio di ciò che ho fatto.

// 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 );
    }
}
È stato utile?

Soluzione

Penso che la risposta nel tuo caso sia quella di utilizzare i dati Bitmap contenuti nell'immagine che stai caricando in questo modo:

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

Altri suggerimenti

Puoi anche usare SpriteFactory , una piccola biblioteca che ho scritto appositamente per questo:

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

Il vantaggio qui è che puoi usare subito gli sprite, che verranno automaticamente riempiti con la bitmap una volta caricata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top