Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何在不向后台添加其他子级的情况下更改位图_Actionscript 3_Url_Random_Bitmap_Addchild - Fatal编程技术网

Actionscript 3 如何在不向后台添加其他子级的情况下更改位图

Actionscript 3 如何在不向后台添加其他子级的情况下更改位图,actionscript-3,url,random,bitmap,addchild,Actionscript 3,Url,Random,Bitmap,Addchild,我确切地知道这个函数在做什么,但是我不知道如何更改它,使3个符号只添加一次,而只有位图更改。谢谢你的帮助!这可能很简单,但我似乎无法理解,我是一名中级as3程序员,但我的知识仍有一些漏洞 var randCount:int = 3 function loadImage():void { for(var i:int = 0; i<randCount; i++) { var imgL

我确切地知道这个函数在做什么,但是我不知道如何更改它,使3个符号只添加一次,而只有位图更改。谢谢你的帮助!这可能很简单,但我似乎无法理解,我是一名中级as3程序员,但我的知识仍有一些漏洞

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();
        }
var randCount:int=3
函数loadImage():void
{

对于(var i:int=0;i我猜你是这样问的,因为你只是在以前的位图上添加了更多的位图,而“更改位图”是指添加新的位图

您需要删除以前添加的内容,然后才能添加新内容。例如,一个简单的更改:

    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);
        }
    }
var randCount:int=3
变量位图:数组=[];
函数loadImage():void
{
对于(变量i:int=0;i randCount)
{
var oldp位图:DisplayObject=bitmaps.shift()作为DisplayObject;
removeChild(旧位图);
}
}

只需创建一个新的位图数据并将其分配给。这是处理此问题最经济的方法。(一个位图数据也可以用于多个位图。)

要执行所需操作,您还可以使用
精灵作为图像的容器,每次都可以将其删除以添加新图像。请看以下示例:

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);
}
var nb_图像:int=3,
位图,
//将5px设置为图像之间的垂直边距
img_裕度:int=5,
img_请求:URLRequest,
img_装载机:装载机;
var images_container:Sprite=newsprite();
addChild(图像和容器);
函数remove_all_images():void
{
//删除所有图像
对于(变量i:int=images\u container.numChildren-1;i>=0;i--)
{
图像(i);;
}
}
函数load_images():void
{
删除所有图像();
对于(变量i:int=0;i

希望这能有所帮助。

嘿,谢谢你的快速回复!我收到一个错误,说它无法将数组转换为显示对象。我准确地复制了你的代码,并出现了两个错误。错误:将uint类型的值隐式强制为不相关的flash类型。显示:DisplayObject。错误:将uint类型的值与可能是不相关的类型数组。虽然(bitmaps.length>bitmaps)var{oldBitmap:DisplayObject=bitmaps.unshift();removeChild(oldBitmap);}(斜体部分是问题的根源)是的,我有一个输入错误,这是未经测试的代码。请参阅编辑。谢谢,修复了一个错误。它仍然在关注位图。unshift();错误:将uint类型的值隐式强制为不相关的flash类型。显示:DisplayObject.I是否仍可以使用按钮。addEventListener(MouseeEvent.CLICK,changeBitmap);函数changeBitmap(事件:MouseeEvent):void{loadImage();}当我做swf冻结。除此之外,没有错误,她正在加载它们,所以他的位图数据已经包装在位图中,在两个现有位图之间切换位图数据没有任何好处。交换位图数据不是一个选项?交换位图数据会很好,如果这样可以保存位图的创建,但这不是一个选项例如,你想每次显示3幅图像吗?是的,我已经解决了上面的问题,还有一个问题。如果我想让y值均匀地隔开图像(bmp)的3个实例,我该怎么做呢?不要随机放置我为你的两个问题给出答案,看一看。谢谢!这非常有帮助!!如果可以的话,我会投赞成票!