Pregunta

He buscado en varios recursos sobre este tema, y ??me parece que necesito un cargador para cada Sprite que contiene un archivo de imagen (png).

Estoy tratando de hacer un sistema de representación en mosaico y he creado una cuadrícula de sprites X por Y, pero todos ellos hacen referencia al mismo archivo de imagen. Hay alguna otra manera de hacer esto? (Haga que el sprite comparta el mismo archivo de datos png)

Algún código de muestra de lo que he hecho.

// 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 );
    }
}
¿Fue útil?

Solución

Creo que la respuesta en su caso es utilizar los datos de mapa de bits contenidos en la imagen que está cargando así:

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

Otros consejos

También podría usar SpriteFactory , una pequeña biblioteca que escribí específicamente para esto:

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

La ventaja aquí es que puedes usar los sprites de inmediato, y se llenarán automáticamente con el mapa de bits una vez que se cargue.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top