0

I got this function from internet which slice an image into part like tiles and put them in a array (linear or multidimensional).

---EXAMPLE---

var sliceClips:Array = sliceMovieClip({source:sourceMovieClip, target:tilesContainer, cols:7, rows:5}).linear;

So this is the function:

    private function sliceMovieClip(o:Object):Object
    {
        var returnArray:Array = new Array();
        var arrayLinear:Array = new Array();
        var array2D:Array = new Array();
        var cols:uint = o.cols;
        var rows:uint = o.rows;
        var tileWidth:uint = o.source.width / cols;
        var tileHeight:uint = o.source.height / rows;
        var rect:Rectangle = new Rectangle(0,0,tileWidth,tileHeight);
        var pnt:Point = new Point();
        var imageBMPD:BitmapData = new BitmapData(o.source.width,o.source.height,true,0x000000);
        var imageBMP:Bitmap = new Bitmap(imageBMPD);
        imageBMPD.draw(o.source);

        for (var tY:uint = 0; tY < rows; tY++)
        {
            var arrayRow:Array = new Array();
            for (var tX:uint = 0; tX < cols; tX++)
            {
                var sourceRect:Rectangle = new Rectangle(tX * tileWidth,tY * tileHeight,tileWidth,tileHeight);
                var destPoint:Point = new Point(0,0);
                var tileBMPD:BitmapData = new BitmapData(tileWidth,tileHeight,true,0x000000);
                var tileBMP:Bitmap = new Bitmap(tileBMPD);
                var tileMCL:MovieClip = new MovieClip();
                tileMCL.addChild(tileBMP);
                tileBMPD.copyPixels(imageBMPD, sourceRect, destPoint);
                tileMCL.x = tX * tileWidth;
                tileMCL.y = tY * tileHeight;
                o.target.addChild(tileMCL);
                returnArray.push(tileMCL);
                arrayRow.push(tileMCL);
            }
            array2D.push(arrayRow);
        }
        return {linear:arrayLinear, multi:array2D};
    }

I'm trying to add to stage those part of image sliced into different spot with a function like this (look at the last if):

    private function buildMap(e:Event):void
    {
        for (var i:int=0; i<visAreaY; i++)
        {
            for (var u:int=0; u<visAreaX; u++)
            {
                if ((MapID[i][u]) == 0)
                {
                    var cell:MovieClip = new tile();
                    cell.gotoAndStop(MapID[i][u]+1);
                    cell.x = tileSide * u;
                    cell.y = tileSide * i;
                    addChild(cell);
                }
                if ((MapID[i][u]) == 1)
                {
                    var cell2:MovieClip = new blacksquare();
                    cell2.gotoAndStop(MapID[i][u]+1);
                    cell2.x = tileSide * u;
                    cell2.y = tileSide * i;
                    addChild(cell2);
                }
                if ((MapID[i][u]) == 2)
                {
                    var cell3:MovieClip = sliceClips[2][0];
                    cell3.gotoAndStop(MapID[i][u]+1);
                    cell3.x = tileSide * u;
                    cell3.y = tileSide * i;
                    trace("Obj: " + cell3 + " w:" + cell3.width + " h:" + cell3.height + " pos:" + cell3.x + " " + cell3.y);
                    addChild(cell3);
                }
            }
        }
    }

There is a problem with cell3 (which use the sliced image) cause it seem to use the same image and doesnt duplicate it on my stage, so if i have it 4 time on my map it will only show the image on the last tile.

I was trying to do:

var cell3:MovieClip = new sliceClips[2][0];

but it won't accept my "new" cause its not a class.

Any idea how i can work this out? How can i set my movieclip array to a class or something.. I'm a beginner in as3

4

1 回答 1

0

Given your sliced images use a single Bitmap object, the best you can do with this is clone a bitmap into a new MC.

        if ((MapID[i][u]) == 2)
            {
                var cell3:MovieClip = new MovieClip();
                cell3.addChild(new Bitmap((sliceClips[2][0].getChildAt(0) as Bitmap).bitmapData));
                // main magic happens here ^ we get the bitmapdata of source MC
                // then make a new bitmap AND a new MC with it
                cell3.x = tileSide * u;
                cell3.y = tileSide * i;
                trace("Obj: " + cell3 + " w:" + cell3.width + " h:" + cell3.height + " pos:" + cell3.x + " " + cell3.y);
                addChild(cell3);
            }
于 2013-05-06T08:13:44.387 回答