动作3:片上的位图到瓷砖 [英] Actionscript 3: Slice a Bitmap into tiles

查看:161
本文介绍了动作3:片上的位图到瓷砖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Loader对象的位图数据,我想切片成32 * 32的正方形瓷砖使用。什么是最有效的方法,这样做?

I have the Bitmap data from a Loader object and I would like to slice it up into 32x32 squares to use as tiles. What is the most efficient way to do so?

推荐答案

我已经做了你的工作​​。的基本思想是使用BitmapData函数copyPixels,请参阅Adobe参考是相同的。它可以让你从源BitmapData一个区域(由矩形指定),在目标BitmapData复制像素的特定点。我创建了一个新的BitmapData每个32×32平方米,并通过循环加载的对象来填充广场与copyPixels。

I have done the work for you. The basic idea is to use the BitmapData function copyPixels, see Adobe Reference for the same . It lets you copy pixels from one region(specified by a Rectangle) in the source BitmapData, to a specific Point in the destination BitmapData. I have created a new BitmapData for each 32x32 square and looped through the loaded object to populate the squares with copyPixels.

var imageLoader:Loader;

function loadImage(url:String):void
{
    // Set properties on my Loader object
    imageLoader = new Loader();
    imageLoader.load(new URLRequest(url));
    imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
}
loadImage("Cow Boy.jpg");//--------->Replace this by your image. I hope you know to specify path. 
function imageLoaded(e:Event):void
{
    // Load Image
    imageArea.addChild(imageLoader);

    var mainImage:BitmapData = new BitmapData(imageArea.width,imageArea.height);
    var tileX:Number = 36; 
    var tileY:Number = 36;
    var bitmapArray:Array;

    var tilesH:uint = Math.ceil(mainImage.width / tileX); // Number  of Columns
    var tilesV:uint = Math.ceil(mainImage.height / tileY);// Number of Rows

    mainImage.draw(imageArea);
    imageArea.x += 500;

    bitmapArray = new Array();

    for (var i:Number = 0; i < tilesH; i++)
    {
        bitmapArray[i] = new Array();
        for (var n:Number = 0; n < tilesV; n++)
        {
            var tempData:BitmapData=new BitmapData(tileX,tileY);
            var tempRect = new Rectangle((tileX * i),(tileY * n),tileX,tileY);
            tempData.copyPixels(mainImage,tempRect,new Point(0,0));
            bitmapArray[i][n]=tempData;
        }
    }

    for (var j:uint =0; j<bitmapArray.length; j++)
    {

        for (var k:uint=0; k<bitmapArray[j].length; k++)
        {

            var bitmap:Bitmap=new Bitmap(bitmapArray[j][k]);
            this.addChild(bitmap);
            bitmap.x = (j+1)* bitmap.width + j*10;
            bitmap.y = (k+1)* bitmap.height + k*10;

        }
    }



}
function imageLoading(e:ProgressEvent):void
{
    // Use it to get current download progress
    // Hint: You could tie the values to a preloader :)
}

这篇关于动作3:片上的位图到瓷砖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆