1

我正在使用flash和as3嘗試獲取構建的照片加載器/查看器/上傳工具。我正在使用文件引用來處理瀏覽/加載/上傳功能。我目前的階段有2個加載器框,我加載用戶選擇的圖像。我需要能夠移除圖像,並在用戶決定後重新選擇另一個圖像。Flash AS3 - DisplayObject參數錯誤 - 嘗試刪除並添加加載的內容

所以在文件加載事件中,我有這樣的代碼:

// Load Image into Editor Function. 
function onMovieClipLoaderComplete(event:Event):void { 
    var loadedContent:DisplayObject=event.target.content; 
    currentLoader =event.target.loader as Loader; 

    currentLoader.x=currentX; 
    currentLoader.y=currentY; 

    currentContent.buttonMode=true; 
    currentContent.addChild(currentLoader); 
    addChild(currentContent); 

    currentLoader.mask = currentMask; 

    addChild(replace_btn); 
} 

而且我代替按鈕i有:

replace_btn.addEventListener(MouseEvent.CLICK, replaceImage); 

function replaceImage(event:Event):void { 
    currentContent.removeChild(currentLoader); 
    removeChild(currentContent); 
} 

在按更換按鈕,一旦它工作正常,但是當我加載另一個圖像加載到加載程序 - 下一次我按下替換按鈕(或任何後續時間)我在我的閃存as3文件中獲得以下參數錯誤:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. 
at flash.display::DisplayObjectContainer/removeChild() 
at MethodInfo-13() 

有誰知道這是什麼意思?奇怪的是,它只發生在第二次起,而不是第一次。我認爲,如果我有: currentContent.addChild(currentLoader); addChild(currentContent); 上加載,然後只是 currentContent.removeChild(currentLoader); removeChild(currentContent); 關於替換功能,它會工作?

全AS3代碼如下所示如果這也有助於

我只學了3-4個月的閃光燈,所以請去容易對我和我道歉,如果我的代碼是不是最好的方式完成了! :)

勞倫

// Set Start Up Values. 
var image1_loader:Loader = new Loader(); 
var image1_content:Sprite = new Sprite(); 
var image1_mask:Sprite = new Sprite(); image1_mask.graphics.beginFill(0x000000,1);   image1_mask.graphics.drawRect(54, 59, 330, 330); image1_mask.graphics.endFill(); 

var image2_loader:Loader = new Loader(); 
var image2_content:Sprite = new Sprite(); 
var image2_mask:Sprite = new Sprite(); image2_mask.graphics.beginFill(0x000000,1);  image2_mask.graphics.drawRect(384, 59, 330, 165); image2_mask.graphics.endFill(); 

var currentBtn; 
var currentLoader; 
var currentContent; 
var currentMask; 
var currentX; 
var currentY; 

replace_btn.visible=false; 


// Define FileReference. 
var canvasImage:FileReference; 


// Image Buttons Function. 
image1_btn.addEventListener(MouseEvent.CLICK, start_fileRef); 
image2_btn.addEventListener(MouseEvent.CLICK, start_fileRef); 

image1_content.addEventListener(MouseEvent.MOUSE_DOWN, setCurrentSelection); 
image2_content.addEventListener(MouseEvent.MOUSE_DOWN, setCurrentSelection); 

function setCurrentSelection(e:MouseEvent):void { 
if (e.currentTarget===image1_content){currentContent=image1_content;} 
if (e.currentTarget===image2_content){currentContent=image2_content;} 
} 


// Browse File Function. 
function start_fileRef(e:MouseEvent):void { 
trace("onBrowse"); 
if (e.target===image1_btn){currentBtn=image1_btn; currentLoader=image1_loader;  currentContent=image1_content; currentMask=image1_mask; currentX=54; currentY=59;} 
if (e.target===image2_btn){currentBtn=image2_btn; currentLoader=image2_loader; currentContent=image2_content; currentMask=image2_mask; currentX=384; currentY=59;} 

canvasImage=new FileReference(); 
canvasImage.addEventListener(Event.SELECT, onFileSelected); 
var imageTypeFilter:FileFilter = new FileFilter("JPG/PNG Files","*.jpeg; *.jpg;*.gif;*.png"); 
canvasImage.browse([imageTypeFilter]); 
} 

// Selected File Function. 
function onFileSelected(event:Event):void { 
trace("onFileSelected"); 
canvasImage.addEventListener(Event.COMPLETE, onFileLoaded); 
canvasImage.addEventListener(ProgressEvent.PROGRESS, onProgress); 

var canvasImageName = canvasImage.name; 
canvasImage.load(); 
} 


// File Progress Function. 
function onProgress(event:ProgressEvent):void { 
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100; 
trace("loaded: "+percentLoaded+"%"); 
} 

// File Loaded Function. 
function onFileLoaded(event:Event):void { 
var fileReference:FileReference=event.target as FileReference; 

var data:ByteArray=fileReference["data"]; 
trace("File loaded"); 
var movieClipLoader:Loader=new Loader(); 
movieClipLoader.loadBytes(data); 
movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieClipLoaderComplete); 

canvasImage.removeEventListener(Event.COMPLETE, onFileLoaded); 
} 

// Load Image into Editor Function. 
function onMovieClipLoaderComplete(event:Event):void { 
var loadedContent:DisplayObject=event.target.content; 
currentLoader =event.target.loader as Loader; 

currentLoader.x=currentX; 
currentLoader.y=currentY; 

currentContent.buttonMode=true; 
currentContent.addChild(currentLoader); 
addChild(currentContent); 

currentLoader.mask = currentMask; 

addChild(replace_btn); 


// Reveal Retry Button over Hover Function // 
currentContent.addEventListener(MouseEvent.ROLL_OVER, hover); 
replace_btn.addEventListener(MouseEvent.ROLL_OVER, hover); 
currentContent.addEventListener(MouseEvent.ROLL_OUT, unhover); 
replace_btn.addEventListener(MouseEvent.ROLL_OUT, unhover); 

function hover(event:Event):void { 
    replace_btn.visible=true; 
} 
function unhover(event:Event):void { 
    replace_btn.visible=false; 
} 

replace_btn.addEventListener(MouseEvent.CLICK, replaceImage); 

function replaceImage(event:Event):void { 
    currentContent.removeChild(currentLoader); 
    removeChild(currentContent); 
} 

} 
+0

目前還不清楚你是否想讓replace_btn有兩個不同的動作(eventlisteners)。如果是這樣,在replaceImage函數中,您應該刪除偵聽器replaceImage,以便在不需要時不調用它。另外,如果有第二個偵聽器,則在添加replaceImage作爲偵聽器之前將其刪除。希望能幫助到你 – Mircea 2012-04-26 11:29:10

回答

0

currentContent.removeChild(currentLoader);應該罰款,但這removeChild(currentContent);最有可能是問題的根源。因爲您將其添加爲currentContent.addChild(currentLoader),所以您也必須使用它刪除容器var(currentContent)。