重复使用图像影片剪辑

时间:2013-02-28 15:16:25

标签: flash actionscript

我想要做的是建立WALL图像,比如2个随机图像。

X0X0X0X0X0(其中X和O是图像。)

现在加载这些我通常会使用for循环,例如(代码不完全,但希望你明白了)

imageArray = "X", "O";

for (x = 0; x < numberOfImagesInWall; x++){

randomNum = random(2)

imageName = imageArray[randomNum];

loadimage (imageName)

.....etc....

}

当我使用这种方法时,我发现它为X和O每次加载图像。我想知道是否有一种方法可以预加载X和O的图像,只需要在任何我想要的时候重复使用它们,其中没有加载是参与。

2 个答案:

答案 0 :(得分:0)

试试这个!

var images:Array = [img1, img2];//u will obviously need to populate this with real images
var pos:int = 0;
var newX:int = 0;

for(var i:int = 0; i < numberOfImagesInWall; i++)
{
    var bd:BitmapData = new BitmapData(images[pos].width, images[pos].height, true, 0x00000000);
    bd.draw(images[pos]); 
    var b:Bitmap = new Bitmap(bd);
    addChild(b);
    b.x = newX;
    newX += b.width;

    if(pos == 0)
        pos++;
    else
        pos = 0; 
}

答案 1 :(得分:0)

var images.Array=[new Image1().bitmapData,new Image2().bitmapData]; // adjust for your case
// this syntax is valid if you have Image1 and Image2 in your library, as bitmap images
for (var i:int=0;i< numberOfImagesInWall; i++)
{
    var b:Bitmap=new Bitmap(images[Math.floor(Math.random()*2.0)]); 
        // provide existing BitmapData as source for the image
    b.x=getX(i);
    b.y=getY(i); // give coordinates as you need it
    addChild(b);
}

将现有BitmapData作为所有位图的链接提供了低内存消耗的优势,并且能够通过引用的BitmapData上的单个操作更改所有相关位图。因此,例如,如果您执行images[0].fillRect(images[0].rect,0xffff0000),那么您的一半有围墙的位图将变为红色,而无需额外的努力。

相关问题