문제

나는 보았다에서 다양한 리소스에 대해 이제,그리고 내가 보기에는 나는 필요로더에 대한 모든 Sprite 를 포함하는 이미지 파일(png).

내가 만들려고 노력하는 타일을 렌더링 시스템,그리고 만든 표 X Y 스프라이트,하지만 그들 모두가 실제로 참조 같은 이미지 파일입니다.할 수 있는 다른 방법이 있습니까?(인 스프라이트를 공유하는 동 png 데이터 파일)

몇 가지 예제 코드의 수행.

// 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 );
    }
}
도움이 되었습니까?

해결책

나는 생각에서 답신의 케이스를 활용 데이터 비트맵 이미지 내에 포함된 당신은 로딩을 다음과 같다:

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

다른 팁

수도 있습 사용 SpriteFactory, 작은 라이브러리를 썼을 위해 특별히:

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

장점이 여기에는 사용할 수 있는 스프라이트는 바로,그들은 자동으로 가득 비트맵면 그것이 로드됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top