如何在可滚动的矩形中放置多个位图? AS3

时间:2015-04-18 21:52:15

标签: actionscript-3 bitmap

此代码构建了一个瓷砖调色板,用于地图制作程序。它接收由其父级设置的数组,并使用该数组中的位图(来自对象)来显示切片网格。现在它只做5x5网格,但是如果我的tileSet中有超过25个tile呢?我想只显示5x5平铺网格,但能够滚动图像。我想我需要制作另一个矩形作为其掩码并使用ScrollBar使其成为scrollRect,但我无法使其工作。请帮助。

public function Palette(X:uint, Y:uint, tileSet:Array) 
    {
        addChild(handleGraphics);

        var palette:Rectangle = new Rectangle(X, Y, 5*32, tileSet.length*32); //Default size is 5x5 tiles.
        handleGraphics.DrawGrid(32,palette.x,palette.y,5,5);

        var counter:int = 0;

        for(var i:int = 0; i < 5; i++)
        {
            paletteArray[i] = [];

            for(var u:int = 0; u < 5; u++)
            {
                if(counter >= tileSet.length)
                {
                    counter = 0; //Which frame to show?
                }
                var b:Bitmap = new Bitmap(tileSet[counter].Graphic);
                b.x = (palette.x) + 32 * u; //Align with palette Rectangle.
                b.y = (palette.y) + 32 * i; ///////////////////////////////
                addChild(b);

                var tileObj:Object = new Object();
                tileObj.Name = tileSet[counter].Name;
                tileObj.Frame = tileSet[counter].Frame;
                tileObj.Graphic = tileSet[counter].Graphic;
                paletteArray[i].push(tileObj);

                setChildIndex(b, 0); //Under grid.
                counter++;
            }
        }
        ActivatePaletteListeners();
    }

此代码适用于少于25个对象的tileSet数组。它循环并连续显示它们直到它达到25.我可以不用这个我猜,但它是一个整洁的影响。


在另一个类(HandleTiles)中,我遍历我的tileSet MovieClip,并使用每个帧为每个tile创建一个新对象。

public function GetPaletteTiles(MC:MovieClip)
    {
        if (tileArray != null)
        {
            tileArray.length = 0;
        }

        for(var i:int = 1; i <= MC.totalFrames; i++)
        {
            MC.gotoAndStop(i); //Change frame for new info.

            var tileObj:Object = new Object(); //The object to push to an array of tiles.
            var graphicData:BitmapData = new BitmapData(32,32);
            graphicData.draw(MC); //Graphic data from sampleTS.

            tileObj.Name = MC.currentFrameLabel;
            tileObj.Frame = MC.currentFrame;
            tileObj.Graphic = graphicData;

            tileArray.push(tileObj);
        }
        BuildIndexArray(15, 20); //Default size 15 x 20.
    }

在这里我将tileSet设置为使用

private function ChangeActiveTileset(Mc:MovieClip)
    {
        activeTileset = Mc;
        GetPaletteTiles(activeTileset);
        UpdatePalette();
    }

我可以用comboBox更改tileSet。这就是我每次调用GetPaletteTiles()时拆掉tileArray的原因。每个tileSet都是不同的MovieClip,如Buildings,Samples,InTheCity等。

1 个答案:

答案 0 :(得分:0)

抱歉,我没有时间早点将这些代码放在一起。这里是平铺代码片段。因为您使用矩形并且必须保持在最大尺寸之下,所以您必须移动源mc。我想你已经知道其他一切了。

//  set the bmp dimensions to device screensize to prevent exceeding device's max bmp dimensions
if (bStagePortrait) {
    iTileWidth = Capabilities.screenResolutionX;
    iTileHeight = Capabilities.screenResolutionY;
} else {
    iTileWidth = Capabilities.screenResolutionY;
    iTileHeight = Capabilities.screenResolutionX;
}

//  mcList.mcListVector is the source mc - a regular mc containing mcs, jpgs, dynamic text, vector shapes, etc.
//  mcList.mcListBmp is an empty mc

aListTiles = new Array();
iNumberOfTiles = Math.ceil(mcList.height / iTileHeight);

for (i = 0; i < iNumberOfTiles; i++) {
    var bmpTile: Bitmap;
    //  move the source mc
    mcList.mcListVector.y = -(i * iTileHeight);
    bmpTile = fDrawTile(mcList, 0, 0, iTileWidth, iTileHeight);
    mcList.mcListBmp.addChild(bmpTile);
    bmpTile.x = 0;
    bmpTile.y = (i * iTileHeight);
    aListTiles.push(bmpTile);
}

//  remove the regular mc
mcList.mcListVector.removeChild(mcList.mcListVector.mcPic);
mcList.mcListVector.mcPic = null;
mcList.removeChild(mcList.mcListVector);
mcList.mcListVector = null;
}


function fDrawTile(pClip: MovieClip, pX: int, pY: int, pWidth: int, pHeight: int): Bitmap {
    trace("fDrawTile: " + pX + "," + pY + "  " + pWidth + "," + pHeight);
    var rectTemp: Rectangle = new Rectangle(pX, pY, pWidth, pHeight);
    var bdClip: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);
    var bdTemp: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);
    bdClip.draw(pClip, null, null, null, rectTemp, true);
    bdTemp.copyPixels(bdClip, rectTemp, new Point(0, 0));
    var bmpReturn: Bitmap = new Bitmap(bdTemp, "auto", true);
    return bmpReturn;
}