我有以下代码允许用户从他们的计算机将图像加载到舞台上,然后将该图像放入新的Sprite图层中:
var imageBrowse:FileReference = new FileReference();
BrowseButton.addEventListener(MouseEvent.MOUSE_DOWN, loadImage);
function loadImage(e:MouseEvent):void{
imageBrowse.browse([new FileFilter("Images", "*.jpg;*.png")]);
imageBrowse.addEventListener(Event.SELECT, fileSelected);
}
function fileSelected(e:Event):void{
imageBrowse.addEventListener(Event.COMPLETE, imageLoaded);
imageBrowse.load();
imageBrowse.removeEventListener(Event.SELECT, selectedFile);
}
function imageLoaded(e:Event):void{
var imageLoader:Loader = new Loader();
imageLoader.loadBytes(e.target.data);
imageLayer.addChild(imageLoader);
imageLoader.x = 375;
imageLoader.y = 175;
imageBrowse.removeEventListener(Event.COMPLETE, imageLoaded);
}
如何对其进行编码,以便每次上传图片时(通过点击BrowseButton
),图片都会添加到新的Sprite
图层(也许imageLoader1
,{ {1}},可以是exmaple图层的名称。我只是不确定如何循环并在每次实际上传图像时创建新的Sprite图层,或者甚至是可能的。
显然,我现在拥有它的方式是,任何上传的图像都放在同一个图层上,并放在同一个地方,这意味着上传的图片不止一张,图像只是叠加在一起作为一个图像'图像',无法独立移动它们。
任何帮助将不胜感激。感谢您的回复。
答案 0 :(得分:0)
var imageBrowse:FileReference = new FileReference();
var myLoaders:Array = [];
BrowseButton.addEventListener(MouseEvent.MOUSE_DOWN, loadImage);
function loadImage(e:MouseEvent):void{
imageBrowse.browse([new FileFilter("Images", "*.jpg;*.png")]);
imageBrowse.addEventListener(Event.SELECT, fileSelected);
}
function fileSelected(e:Event):void{
imageBrowse.addEventListener(Event.COMPLETE, imageLoaded);
imageBrowse.load();
//imageBrowse.removeEventListener(Event.SELECT, selectedFile);
}
function imageLoaded(e:Event):void{
//the length of the array is initially 0, so the first time it will be imageLoaders[0]
//the intractive object needs to be a movieclip loader do not have start/stop drag methods
myLoaders[myLoaders.length] = new MovieClip();//add clip to array
//create a temporary loader
var loader = new Loader();
loader.loadBytes(e.target.data)
//add the loader to the movieclip
myLoaders[myLoaders.length-1].addChild(loader);
//make the movieclip to the convention you specified
myLoaders[myLoaders.length - 1].name = "imageLoader" + (myLoaders.length - 1).toString();
//attach the moviclip to the imageLayer
imageLayer.addChild(myLoaders[myLoaders.length-1]);
myLoaders[myLoaders.length-1].x = 375;
myLoaders[myLoaders.length-1].y = 175;
myLoaders[myLoaders.length-1].addEventListener(MouseEvent.MOUSE_DOWN, onImageDown);
myLoaders[myLoaders.length-1].addEventListener(MouseEvent.MOUSE_UP, onImageUp);
imageBrowse.removeEventListener(Event.COMPLETE, imageLoaded);
}
function onImageDown(event:MouseEvent):void
{
//bring the image that you clicked to the top
imageLayer.setChildIndex(event.currentTarget as DisplayObject, imageLayer.numChildren -1);
event.currentTarget.startDrag();
}
答案 1 :(得分:0)
创建“新图层”的想法是创建一个加载器,然后将其添加到影片剪辑中,然后将其添加到imageLayer对象,该对象可以是舞台或指定的显示对象,用作帆布。要将所选对象置于最前端,可以将imageLayer中的索引更改为可用的最高值。
//create variables
var imageBrowse:FileReference = new FileReference();
var imageCount:int = 0;
BrowseButton.addEventListener(MouseEvent.MOUSE_DOWN, loadImage);
function loadImage(e:MouseEvent):void{
imageBrowse.browse([new FileFilter("Images", "*.jpg;*.png")]);
imageBrowse.addEventListener(Event.SELECT, fileSelected);
}
function fileSelected(e:Event):void{
imageBrowse.addEventListener(Event.COMPLETE, imageLoaded);
imageBrowse.load();
//imageBrowse.removeEventListener(Event.SELECT, selectedFile);
}
function imageLoaded(e:Event):void{
var clip = new MovieClip();
var loader = new Loader();
loader.loadBytes(e.target.data)
clip.addChild(loader);
//make the movieclip to the convention you specified
clip.name = "imageLoader" + (imageCount++).toString();
//attach the moviclip to the imageLayer
imageLayer.addChild(clip);
clip.x = 375;
clip.y = 175;
clip.addEventListener(MouseEvent.MOUSE_DOWN, onImageDown);
clip.addEventListener(MouseEvent.MOUSE_UP, onImageUp);
imageBrowse.removeEventListener(Event.COMPLETE, imageLoaded);
}
function onImageDown(event:MouseEvent):void
{
//bring the image that you clicked to the top
imageLayer.setChildIndex(event.currentTarget as DisplayObject, imageLayer.numChildren -1);
event.currentTarget.startDrag();
}
function onImageUp(event:MouseEvent):void
{
event.currentTarget.stopDrag();
}