1

我正在使用 flash 和 as3 来尝试构建照片加载器/查看器/上传工具。我正在使用文件引用来处理浏览/加载/上传功能。我当前的阶段有 2 个加载器框,我将用户选择的图像加载到其中。不过,如果用户决定,我需要能够删除图像并重新选择另一个图像。

所以在文件加载事件中我有这个代码:

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

在我的替换按钮上,我有:

replace_btn.addEventListener(MouseEvent.CLICK, replaceImage);

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

一旦按下替换按钮,它工作正常,但是当我将另一个图像加载到加载器中时 - 下次我按下替换按钮(或任何后续时间)时,我的 flash 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 代码如下所示

我只学习 Flash 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);
}

}
4

1 回答 1

0

currentContent.removeChild(currentLoader);应该没问题,但这 removeChild(currentContent);很可能是问题的根源。因为您将它添加为currentContent.addChild(currentLoader),所以您也必须删除容器 var (currentContent) 。

于 2012-05-23T15:19:28.430 回答