ActionScript 3在循环中更改位图源

时间:2017-07-22 23:27:17

标签: actionscript-3 flash

我有一个叫做RenderingImage的类,它是一个像这样的位图:

[Embed(source="images/thumb.png")]
var RenderingImage:Class;

我要做的是在循环中更改位图图像。

for (var i:String in homes) 
{
    var renderingImg:Bitmap = new RenderingImage();
    renderingImg.y = 50 + spaceY;
    renderingImg.x = 0 + spaceX;
    renderingImg.width = 320;
    renderingImg.height = 185;
    home.addChild(renderingImg);
}

在这个循环中定义RenderingImage类时,有没有改变我放的源?

1 个答案:

答案 0 :(得分:0)

最好不要在项目中嵌入图像资源。无论您如何存储图像,只需在循环时指向新图像。

var images:Array = [
    "https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
    "https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
    "https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]

for (var i:int = 0; i < images.length; i++) {
    var house:Loader = new Loader();
    house.load(new URLRequest(images[i]));
    addChild(house)
    house.x = 65*i;
}

或者,如果您只使用明确的Bitmap个对象,则可以在加载后引用加载的BitmapData

import flash.events.Event;
import flash.display.Loader;
import flash.display.Bitmap;

var images:Array = [
    "https://cdn1.iconfinder.com/data/icons/freeline/32/home_house_real_estate-64.png",
    "https://cdn2.iconfinder.com/data/icons/cute-tech-icon-set-1/512/Home-64.png",
    "https://cdn3.iconfinder.com/data/icons/watchify-v1-0-80px/80/home-80px-64.png"
]
var bitmaps:Array = [];
var loadedCount:int = 0;

for (var i:int = 0; i < images.length; i++) {
    var loader:Loader = new Loader();
    loader.load(new URLRequest(images[i]));
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}

function loaded(e:Event):void {
    bitmaps.push(e.currentTarget.loader.content);
    loadedCount++;

    if (loadedCount == images.length) {
        for (var i:int = 0; i < images.length; i++) {
            var houseBmp:Bitmap = new Bitmap();
            houseBmp.bitmapData = bitmaps[i].bitmapData;
            houseBmp.x = 65*i;
            addChild(houseBmp);
        }
    }
}