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)。