从动画片段AS 3.0中删除图片

时间:2013-11-03 13:47:14

标签: image actionscript-3 flash mask movieclip

我使用下面的代码制作了一个组合flash。 它工作正常,但我有一个问题。 由于图片具有不同的尺寸,因此当加载较小的图片时,先前的图像仍然在背景中。 你能告诉我如何只显示清除背景的当前图片。

提前致谢

import flash.display.BitmapData;
var loader:Loader;
var loading:TextField = new TextField();
var image:Bitmap;
var xmlLoader:URLLoader = new URLLoader();
var files:Array= new Array();
var titles:Array= new Array();
var description:Array= new Array();
var index:int = 0;
var pictures:int = 0;

xmlLoader.load(new URLRequest("images.xml"));
xmlLoader.addEventListener(Event.COMPLETE, readXML);

function readXML(e:Event):void {

XML.ignoreWhitespace = true;
var images:XML = new XML(e.target.data);
pictures = images.picture.length();
for (var i:Number=0; i<pictures; i++) {
    titles[i]=images.picture[i].title.text();
    files[i]=images.picture[i].file.text();
    description[i]=images.picture[i].description.text();
}
load(0);
}

function load(index:int):void {

circle.visible = false;

loader=new Loader();
loader.load(new URLRequest(files[index]));

text_folio.text = titles[index];
description_folio.text = description[index];

//loader.contentLoaderInfo.addEventListener("progress",progressLoad);
loader.contentLoaderInfo.addEventListener("complete",completeLoad);
}

function progressLoad(e:Event):void {
loading.x=100;
loading.y=100;
stage.addChild(loading);
loading.text="Loading: ";

var tFormat:TextFormat=new TextFormat();
tFormat.size = 20;
tFormat.color = 0x888888;
loading.setTextFormat(tFormat);
}

function completeLoad(e:Event):void {
bigImg.mask = null;
image=Bitmap(loader.content);
bigImg.addChild(image);

//create the small image from the big image
var bmp:BitmapData = new BitmapData(bigImg.width, bigImg.height);
bmp.draw(bigImg);

var bitmap2:Bitmap = new Bitmap(bmp);
bitmap2.scaleX /=2;
bitmap2.scaleY /=2;
smallImg.addChild(bitmap2);

loader.contentLoaderInfo.removeEventListener("progress",progressLoad);
loader.contentLoaderInfo.removeEventListener("complete",completeLoad);

loader=null;

loading.text="";
loading.visible=false;

circle.visible = true;
bigImg.mask = circle;//add the circle mask to the big image
stage.addEventListener("enterFrame",mGlass);

}

function mGlass(e:Event) {
bigImg.x = (mouseX * -1);
bigImg.y = (mouseY * -1);

circle.x = (mouseX-150);
circle.y = (mouseY-150);
}

left.addEventListener(MouseEvent.CLICK, buttonLeft);
right.addEventListener(MouseEvent.CLICK, buttonRight);

function buttonRight(event:MouseEvent):void {
if (index == pictures-1)
    index = 0;
else
    index++;
load(index);
}

function buttonLeft(event:MouseEvent):void {
if (index == 0)
    index = pictures-1;
else
    index--;
load(index);
}

1 个答案:

答案 0 :(得分:0)

我模拟了你的设置,这就是我想出来的。

此代码将查看bigImg内部并查看其中有多少个显示子项,然后遍历所有子项并删除它们。 (这种方法假设bigImg Movieclip中没有其他内容)。请问你是否不完全确定它是如何工作的,但它应该是合理的自我解释。

for (var i:int = 0; i < bigImg.numChildren; i++) {
    bigImg.removeChildAt(i);
}

在进行load调用之前,应将其添加到loader.load函数中。

function load(index:int):void {
    var i:int;
    for (i = 0; i < bigImg.numChildren; i++) {
        bigImg.removeChildAt(i);
    }
    circle.visible = false;
    loader=new Loader();
    loader.load(new URLRequest(files[index]));
    text_folio.text = titles[index];
    description_folio.text = description[index];    
    loader.contentLoaderInfo.addEventListener("progress",progressLoad);
    loader.contentLoaderInfo.addEventListener("complete",completeLoad);
}