Flash 每次单击按钮时创建新层
我有以下代码,允许用户从计算机将图像加载到舞台上,然后将该图像放置到新的精灵层中:Flash 每次单击按钮时创建新层,flash,actionscript,Flash,Actionscript,我有以下代码,允许用户从计算机将图像加载到舞台上,然后将该图像放置到新的精灵层中: var imageBrowse:FileReference = new FileReference(); BrowseButton.addEventListener(MouseEvent.MOUSE_DOWN, loadImage); function loadImage(e:MouseEvent):void{ imageBrowse.browse([new FileFilter("Images", "
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);
}
如何对其进行编码,以便每次上传图像时(通过单击浏览按钮
)将图像添加到新的精灵
层(可能图像加载程序1
,图像加载程序2
,可能是exmaple层的名称)。我只是不知道如何在每次上传图像时循环并创建一个新的精灵层,或者是否可能
显然,我现在的做法是,任何上传的图像都被放在同一层和同一个地方,这意味着上传的多个图像最终会以一个“图像”的形式堆叠在一起,无法独立移动它们 任何帮助都将不胜感激。感谢您的回复。创建“新图层”的想法是创建一个加载程序,然后将其添加到电影剪辑中,然后将其添加到imageLayer对象中,该对象可以是舞台或指定的显示对象,您可以将其用作画布。要将选定对象置于最前端,可以将其在imageLayer中的索引更改为可用的最高索引
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();
}
//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();
}
对于表示
var-imageLoaders:Array=[]
I get1086:语法错误:左括号前应为分号。
Ah否,错误是针对行var imageLoaders[imageLoaders.length]=new Loader()代码>确定。我已经更新了它,我想它已经修复了。看起来旧的数组名(imageLoaders)是flash中的保留函数。那是在扔东西。还请注意,我在命名新加载程序时添加了一个toString()方法。谢谢,但现在发生的是,当我上载多个图像时,第二个图像正好覆盖在另一个图像上,我无法单独移动它们(使用startDrag
和stopDrag
)问题在于加载程序一直被添加到imageLayer
,而不是将每个加载程序图像添加到新层,但我不确定如何将加载程序添加到尚不存在的新sprite层?我声明了如下层:var-imageLayer:Sprite=newsprite
然后通过执行addChild(imageLayer)
添加它们,因为某些原因它不起作用;我上传的第二张图片就在第一张图片的上方,位置相同,我无法单独移动它们;两个图像相互重叠并成为一个。很明显,我现在的做法是,任何上传的图像都被放置在同一层和同一个位置,这意味着上传的多个图像最终会以一个“图像”堆叠在一起,无法独立移动。好的。因此,如果您想将所有图像移动到一起,请忽略向电影剪辑中添加星号/停止拖动,甚至包括侦听器。将启动/拖放事件侦听器添加到imageLayer(应该是movieclip)中,这样就可以了。你有我可以与你联系的电子邮件地址吗?我认为您最好查看代码并运行它(如果您不介意的话),这样您就可以看到我做了什么以及我想做什么。