缓存和渲染spritesheets不起作用

时间:2011-10-25 23:20:09

标签: arrays actionscript-3 caching bitmapdata

我想将我的spritesheet(tilesheet)缓存到一个数组中。我这样做是因为每个spritesheet都应该缓存在一个数组中,这样我的对象就可以轻松地从它们中拉出它们的tile。但我的缓存似乎不起作用,因为绝对没有任何东西被渲染出来。我看不到任何东西。

我的缓存中有一些内容(可能是bitmapData),它不是null,所以目前我不知道问题出在哪里。

有人可以帮我解决这个问题吗?

此函数应通过copypixels将数组的每个图块渲染为背景

public function renderCachedTile(canvasBitmapData:BitmapData,    tileArray:Array):void
    {

        tileCache = tileArray;

        tileArray = [];

        x = nextX;
        y = nextY;

        point.x = x;
        point.y = y;

        tileRect.x = tileWidth;
        tileRect.y = tileHeight;

        if (animationCount >= animationDelay)
        {
            animationCount = 0;

            if(reverse)
            {
                currentTile--;
                if (currentTile < 1)
                {
                    currentTile = tilesLength - 1;
                }


            } else {

                currentTile++;
                if (currentTile >= tilesLength - 1)
                {
                    currentTile = 0;
                }


            }

        } else {
            animationCount++;
        }

        canvasBitmapData.lock();    


        canvasBitmapData.copyPixels(tileCache[currentTile], tileRect, point);
        canvasBitmapData.unlock();


    }

这个函数'将'spritesheet'分隔成tile并将它们抛出一个数组

private function tileToCache():void {
        var tileBitmapData:BitmapData = new BitmapData(tileWidth, tileHeight, true, 0x00000000);
        var tilePt:Point = new Point(0, 0);
        var tileRect:Rectangle = new Rectangle;


        tileCache = [];

        for (var tileCtr:int = 0; tileCtr < tilesLength; tileCtr++) {

            tileBitmapData.lock();
            tileRect.x = int((tileCtr % spritesPerRow)) * tileWidth;
            tileRect.y = int((tileCtr / spritesPerRow)) * tileHeight;

            tileBitmapData.copyPixels(tileSheet, tileRect, tilePt);
            tileBitmapData.unlock();

            tileCache.push(tileBitmapData);

        }
}

1 个答案:

答案 0 :(得分:1)

除非您的示例中缺少代码,否则在tileToCache中实例化并使用tileRect而不定义宽度和高度:

private function tileToCache():void {
    var tileRect:Rectangle = new Rectangle;

    /* ... */
    tileBitmapData.copyPixels(tileSheet, tileRect, tilePt);

如果你的来源是0,0,我认为你会想要:

var tileRect:Rectangle = new Rectangle(0, 0, tileWidth, tileHeight);