AS3如何删除previous装载机 [英] AS3 How to remove previous loaders

查看:99
本文介绍了AS3如何删除previous装载机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flash CS4中,我创建一个照相馆。我的目标是从一些图像的加载不同的缩略图。我已成功,当一个点击的图像,正在显示许多缩略图,但是当一个点击另一个图像,则新的缩略图被放置在的旧的顶部。有人可以帮助我如何摆脱旧的缩略图?

下面是code:

 的(VAR我:= 0; I< thumbnails.length();我++){
  imgLoader.unload();
  imgLoader =新的Loader();
  imgLoader.load(新的URLRequest(缩略图[I]));
  imgLoader.name =我;
  imgLoader.x = 95 *列;
  imgLoader.y = 80 *行;
  imgLoader.alpha = 0;
  details.gallery.addChild(imgLoader);

  如果(列+1小于5){
    列++;
  } 其他 {
    列= 0;
    行++;
  }
}
 

解决方案

这是一个数组是你的朋友。您可以通过只使用一个while循环,从你加入了一个大拇指的Sprite或MovieClip删除每一个最后的孩子做到这一点没有一个数组。我们使用数组的原因是,使我们可以重新使用了大拇指,而不是重新加载它们,我们只是从显示列表中删除。你推一个引用的每个对象到一个数组中的每个拇指,你将它添加到显示列表中。 XML中的每个thumbContainer节点都有其自己的阵列,其被添加到主阵列。主数组保存引用缩略图阵列。缩略图阵列持有引用加载的缩略图,以便它们可以被添加,并从显示列表中删除。如果您计划从来不使用大拇指,他们已经见过一次后,你可以设置它的值等于空,否则仅仅从显示列表中删除;没有任何理由来加载了很多次。当您准备增加新的拇指必须清除出previous大拇指。要做到这一点,最简单的方法是使用whil​​e循环。

  //假设大拇指被装入集装箱
而(container.numChildren大于0)
{
    //删除第一个孩子,直到有没有。
    container.removeChildAt(0);
}

//将XML / 2瓶/ thumbContainer [0]和thumbContainer [1]
 

 < XML版本=1.0编码=UTF-8&GT?;
< XML>
    < thumbContainer>
        <拇指路径=路径/到/文件/>
        <拇指路径=路径/到/文件/>
        <拇指路径=路径/到/文件/>
    < / thumbContainer>
    < thumbContainer>
        <拇指路径=路径/到/文件/>
        <拇指路径=路径/到/文件/>
        <拇指路径=路径/到/文件/>
    < / thumbContainer>
< / XML>
 

 包
{
    进口flash.display.Sprite;
    进口对象类型:flash.events.Event;
    进口flash.events.MouseEvent;
    进口flash.net.URLLoader;
    进口flash.net.URLRequest;

    公共类的DocumentClass扩展Sprite
    {
        私人VAR _container:雪碧;
        私人VAR _mainArray:阵列;
        私人VAR _xml:XML;
        私人VAR _urlLoader:的URLLoader;
        私人VAR _urlRequest:的URLRequest;

        公共职能的DocumentClass():无效
        {
            如果(阶段)_init();
            其他的addEventListener(Event.ADD_TO_STAGE,_init,假,0,真正的);
        }
        私有函数_init(E:事件= NULL):无效
        {
            //将包含在XML阵列为每个thumbContainer。
            _mainArray = [];

            _urlRequest =新的URLRequest(路径/到/ XML');
            _urlLoader =新的URLLoader();
            _urlLoader.addEventListener(引发Event.COMPLETE,_onXMLComplete,假,0,真正的);
        }
        私有函数_onXMLComplete(五:事件):无效
        {
            _xml =新的XML(e.target.data);

            _loadThumbs(0);
        }
        私有函数_loadThumbs(pIndex:INT):无效
        {
            _clearThumbs();

            //找出多少套拇指那里,并加入到_mainArray
            对于(VAR我:= 0; I< _xml.thumbContainer.length();我++)
            {
                VAR tempArray:阵列=新的Array();

                对于(VAR记者:INT = 0; J< _xml.thumbContainer [I] .thumb.length; J ++)
                {
                    tempArray [I] .push(_xml.thumbContainer [I] .thumb [J] @路径。);
                }
                _mainArray.push(tempArray);
            }


            //这里就是我们添加了新的内容的容器,也可以调用一个函数来做到这一点。
        }
        私有函数_clearThumbs():无效
        {
            而(container.numChildren大于0)
            {
                //删除第一个孩子,直到有没有。
                container.removeChildAt(0);
            }
        }
    }
}
 

再次,这是很好的做法,以保持一个对的东西是可重复使用的,并简单地将其从显示列表中删除,而不是设置为空,prepping垃圾回收只待稍后再加载。我已经写了超过我的本意,却不能在所有的code,我想一巴掌。重要的是要设置code表示确保它仅仅加载特定的一组大拇指一次;这是整体思路。至于删除它们,就这么简单的while循环我给你,你只需要知道DisplayObjectContainer的名字,父母他们。

布赖恩·霍奇
hodgedev.com
blog.hodgedev.com

In Flash CS4, I'm creating a photo gallery. My goal is to load different thumbnails from a number of images. I have managed that when one clicks an image, a number of thumbnails are being displayed, but when one clicks another image, the new thumbnails are placed on top of the old ones. Can someone help me on how to get rid of the old thumbnails?

Here is the code:

for (var i:int = 0; i < thumbnails.length(); i++) {
  imgLoader.unload();
  imgLoader = new Loader();
  imgLoader.load(new URLRequest(thumbnails[i]));
  imgLoader.name= i;
  imgLoader.x =  95 * columns;
  imgLoader.y = 80 * rows;
  imgLoader.alpha = 0;
  details.gallery.addChild(imgLoader);

  if (columns+1< 5) {
    columns++;
  } else {
    columns = 0;
    rows++;
  }
}

解决方案

This is where an Array is your friend. You could do this without an array by merely using a while loop to remove every last child from the sprite or movieclip that you added the thumbs to. The reason we use arrays is so that we can reuse the thumbs, instead of reloading them we merely remove them from the display list. You push a reference to each object into an array for each thumb as you add it to the display list. Each thumbContainer node in the XML gets its own array which get added to the main array. The main array holds references to thumbnail arrays. Thumbnail arrays hold references to loaded thumbnails so that they can be added and removed from the display list. If you plan to never use the thumbs after they have been seen once you may set it's reference equal to null, otherwise merely remove it from the display list; There is no reason to load it many times. When you are ready to add the new thumbs you must clear out previous thumbs. The easiest way to do this is with a while loop.

//Assuming the thumbs were loaded into container
while(container.numChildren > 0)
{
    //Remove the first child until there are none.
    container.removeChildAt(0);
}

//The XML / 2 Containers / thumbContainer[0] and thumbContainer[1]

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <thumbContainer>
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
    </thumbContainer>
    <thumbContainer>
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
        <thumb path="path/to/file" />
    </thumbContainer>
</xml>

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class DocumentClass extends Sprite
    {
        private var _container:Sprite;
        private var _mainArray:Array;
        private var _xml:XML;
        private var _urlLoader:URLLoader;
        private var _urlRequest:URLRequest;

        public function DocumentClass():void
        {
            if(stage) _init();
            else addEventListener(Event.ADD_TO_STAGE, _init, false, 0 , true);
        }
        private function _init(e:Event = null):void
        {
            //Will contain arrays for each thumbContainer in the XML.
            _mainArray = [];

            _urlRequest = new URLRequest('path/to/xml');
            _urlLoader = new URLLoader();
            _urlLoader.addEventListener(Event.COMPLETE, _onXMLComplete, false, 0, true);      
        }
        private function _onXMLComplete(e:Event):void
        {
            _xml = new XML(e.target.data);

            _loadThumbs(0);
        }
        private function _loadThumbs(pIndex:int):void
        {
            _clearThumbs();

            //Find out how many sets of thumbs there and add to _mainArray
            for(var i:int = 0; i < _xml.thumbContainer.length(); i++)
            {
                var tempArray:Array = new Array();

                for(var j:int = 0; j < _xml.thumbContainer[i].thumb.length; j++)
                {
                    tempArray[i].push(_xml.thumbContainer[i].thumb[j].@path);
                }
                _mainArray.push(tempArray);
            }


            //Here is where we add the new content to container, or you can call a function to do it.
        }
        private function _clearThumbs():void
        {
            while(container.numChildren > 0)
            {
                //Remove the first child until there are none.
                container.removeChildAt(0);
            }
        }
    }
}

Again, it is good practice to hold a reference to something that is reusable and to simply remove it from the display list instead of setting to null and prepping for garbage collection only to be loaded again later. I already have written more than I intended and wasn't able to slap in all the code I wanted. It is important to setup the code that makes sure it only loads a particular set of thumbs once; That is the whole idea. As for removing them, it's as simple as the while loop I showed you, you just need to know the name of the DisplayObjectContainer that parents them.

Brian Hodge
hodgedev.com
blog.hodgedev.com

这篇关于AS3如何删除previous装载机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆