我确切知道这个函数在做什么,但是我不知道如何更改它,以便3个符号只添加一次,只有位图发生变化。谢谢您的帮助!它可能很简单,但我似乎无法弄明白,我是一名中级程序员,但我的知识仍然有一些漏洞。
var randCount:int = 3
function loadImage():void
{
for(var i:int = 0; i<randCount; i++)
{
var imgLoader:Loader = new Loader();
var imgRequest:URLRequest = new URLRequest();
imgRequest.url = "../img/planet" + int(2*Math.random()) +".png";
trace(imgRequest.url);
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
}
}
function onLoadedImg(e:Event):void
{
e.currentTarget.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bmp.x = 600;
bmp.y = Math.random() * 520;
bmp.width = 80;
bmp.height = 80;
addChild(bmp);
}
button.addEventListener(MouseEvent.CLICK, changeBitmap);
function changeBitmap(event:MouseEvent):void {
loadImage();
}
答案 0 :(得分:0)
猜测你问这个问题是因为你只是在前面的位置上添加了更多的位图,而通过“更改位图”你的意思是添加新的位图。
您需要删除之前添加的内容,然后才能添加新内容。例如,一个简单的改变:
var randCount:int = 3
var bitmaps:Array = [];
function loadImage():void
{
for(var i:int = 0; i<randCount; i++)
{
var imgLoader:Loader = new Loader();
var imgRequest:URLRequest = new URLRequest();
imgRequest.url = "../img/planet" + int(2*Math.random()) +".png";
trace(imgRequest.url);
imgLoader.load(imgRequest);
imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
}
}
function onLoadedImg(e:Event):void
{
e.currentTarget.removeEventListener(Event.COMPLETE, onLoadedImg);
var bmp:Bitmap = e.currentTarget.content;
bitmaps.push(bmp);
bmp.x = 600;
bmp.y = Math.random() * 520;
bmp.width = 80;
bmp.height = 80;
addChild(bmp);
while(bitmaps.length > randCount)
{
var oldbitmap:DisplayObject = bitmaps.shift() as DisplayObject;
removeChild(oldbitmap);
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
要执行您要查找的内容,您还可以使用Sprite
作为图像的容器,并且每次都可以删除它们以添加新图像。看看这个例子:
var nb_images:int = 3,
bmp:Bitmap,
// set 5px as vertical margin between images
img_margin:int = 5,
img_request:URLRequest,
img_loader:Loader;
var images_container:Sprite = new Sprite();
addChild(images_container);
function remove_all_images(): void
{
// remove all images
for(var i:int = images_container.numChildren - 1; i >= 0; i--)
{
images_container.removeChildAt(i);
}
}
function load_images(): void
{
remove_all_images();
for(var i:int = 0; i < nb_images; i++)
{
// to generate randomly values between 0 and n (excluded) we use : int(n * Math.random())
img_request = new URLRequest('images/' + (int(nb_images * Math.random())) + '.png')
img_loader = new Loader();
img_loader.load(img_request);
img_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_img_loaded);
}
}
function on_img_loaded(e:Event): void
{
e.currentTarget.removeEventListener(Event.COMPLETE, on_img_loaded);
bmp = e.currentTarget.content;
bmp.smoothing = true;
bmp.x = 0, bmp.width = bmp.height = 80;
// set image.y
// 1st image : y = 0, 2nd image : y = 80 + 5 = 85, 3rd image : y = 2 x 85 = 170
bmp.y = images_container.numChildren * (bmp.height + img_margin);
// add the image to images_container
images_container.addChild(bmp);
}
希望可以提供帮助。