2013-11-03 41 views
0

我用下面的代碼由組合閃光。 它工作正常,但我有一個問題。 由於圖片具有不同的尺寸,當一個較小的圖片被加載之前的圖像仍然在背景中。 你能幫我告訴我如何只顯示清除背景的當前圖片。從一個movieclip消除照片AS 3.0

在此先感謝

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); 
} 

回答

0

我做了一個模擬了你設置的,這是我想出來的。

此代碼將看看裏面bigImg,看看它有多少孩子顯示有它,然後通過所有這些循環和刪除它們。 (此方法假定bigImg Movieclip中沒有其他內容)。請問你是否不完全確定它是如何工作的,但它應該是合理的自我解釋。

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

這應該使loader.load調用之前被添加到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); 
}