Removing parent.AddChild added in Loop

那年仲夏 提交于 2019-12-11 14:17:43

问题


@Yaroslav Mate

I moved the question to this new one. I am trying to remove the loading111 when the image is loaded. What i am trying to do, to add loading111 into each Bolder, and when the image is loaded, and i remove the loading111(which is just a sign showing loading).

Thanks for the help!

for (var k:int = 0; k < test.length; k++) 
            {
                var Bolder:Listing8 = new Listing8();

                Bolder.x=20;





                if(test[k].pic1){
                        var loader:Loader = new Loader();
   var loading111:loading1 = new loading1;
   Bolder.addChild(loading111);
                    loader.load(new 

            URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,  onImageLoaded);


        Bolder.addChild(loader);    

                    }





            function onImageLoaded(e:Event):void {



                        e.currentTarget.loader.content.height =50;


e.target.content.x = 130;
                e.target.content.y = 15;
                e.target.content.height = 70;
                e.target.content.width = 140;






                var bf:TextField = new TextField();
                var bf1:TextField = new TextField();
                var bf2:TextField = new TextField();
                var bf3:TextField = new TextField();



                bf3.width = 100;
                bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                bf.width = 100;
                bf.autoSize = TextFieldAutoSize.CENTER;
                bf1.width = 100;
                bf1.autoSize = TextFieldAutoSize.CENTER;
                bf2.autoSize = TextFieldAutoSize.CENTER;
                bf3.autoSize = TextFieldAutoSize.CENTER;
                bf3.width = 100;
                bf1.y= bf.height+5;



                    // Pulling the textfields content out from the current bookVO

                bf.text = test[k].nobed;
                bf1.text = test[k].zip;
                bf2.text = test[k].Location;
                bf3.text = test[k].price;

                bf.x = (Bolder.height-bf.height)*.2
                Bolder.addChild(bf);
                Bolder.addChild(bf1);
                Bolder.addChild(bf2);
                Bolder.addChild(bf3);






                    // position the object based on the accumulating variable.
                Bolder.y = currentY;



                Bolder.mouseChildren = false;    // ignore children mouseEvents
                Bolder.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
                Bolder.useHandCursor = true;     // add hand cursor on mouse over
                Bolder.buttonMode = true; 
                trace ('add image with index', k);
                image[k]= new Sprite();
                image[k].addChild(Bolder);
                image.mouseChildren = true;    // ignore children mouseEvents
                image.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
                image.useHandCursor = true;     // add hand cursor on mouse over
                image.buttonMode = true; 
                image[k].addEventListener(MouseEvent.CLICK, gotoscene);

                currentY += Bolder.height + 10;

            }








            if( test.length > 0 )
             {
                _contentHolder = new Sprite();


                addChild(_contentHolder);






       for (var j:int = 0; j < test.length; j++) {



                _contentHolder.addChild(image[j]);



    } 

Edit:

package  com.clark
{
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.display.Sprite;
    import flash.display.*;
    public class SearchVectorTest extends MovieClip 
    {
        public var listings:Vector.<Listing8>;
            public var _contentHolder: Sprite= new Sprite;;
        public var id:String;
         public var nobed:String;
        public var zip:String;
        public var Location:String;
        public var price:String;
        public var pic1:String;
        public var pic2:String;
        public var pic3:String;
        public var loadedArray:Array = new Array;
           public var callMethod:Function;
        private var image:Array = new Array;
        private var sprite:Sprite = new Sprite;
        public var newArray:Array = new Array;
        public var Bolder:Listing8 = new Listing8();

        public  var index:Number;
           public static var lists:Array;


        public function SearchVectorTest(test:Vector.<searchVO1>) 
        {

            GLOBALS.resultholder1 = this;


                for (var j:int = 0; j < test.length; j++) 
            {

                trace(test[j].id);
                trace(test[j].nobed);
                trace(test[j].zip);
                trace(test[j].Location);
                trace(test[j].price);
                trace(test[j].pic1);
                trace(test[j].pic2);
                trace(test[j].pic3);

        }







            var currentY:int = 100;

        for (var k:int = 0; k < test.length; k++) 
            {
                var Bolder:Listing8 = new Listing8();

                Bolder.x=20;



                if(test[k].pic1){
                var loader:Loader = new Loader();
                    var loading111:loading1 = new loading1;
                loader.load(new URLRequest("http://www.rentaid.info/rent/"+test[k].pic1));

                    Bolder.addChild(loading111);
                    Bolder.addChild(loader);
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE,  onImageLoaded);


                }





        function onImageLoaded(e:Event):void {



                e.target.content.x = 130;
                e.target.content.y = 15;
                e.target.content.height = 70;
                e.target.content.width = 140;
            var boulderOfThisImage = e.target.parent;
    while(boulderOfThisImage.numChildren){
        boulderOfThisImage.removeChildAt(0);
        }
    boulderOfThisImage.addChild(e.target);

            }






                var bf:TextField = new TextField();
                var bf1:TextField = new TextField();
                var bf2:TextField = new TextField();
                var bf3:TextField = new TextField();



                bf3.width = 100;
                bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                bf.width = 100;
                bf.autoSize = TextFieldAutoSize.CENTER;
                bf1.width = 100;
                bf1.autoSize = TextFieldAutoSize.CENTER;
                bf2.autoSize = TextFieldAutoSize.CENTER;
                bf3.autoSize = TextFieldAutoSize.CENTER;
                bf3.width = 100;
                bf1.y= bf.height+5;



                    // Pulling the textfields content out from the current bookVO

                bf.text = test[k].nobed;
                bf1.text = test[k].zip;
                bf2.text = test[k].Location;
                bf3.text = test[k].price;

                bf.x = (Bolder.height-bf.height)*.2

                Bolder.addChild(bf);
                Bolder.addChild(bf1);
                Bolder.addChild(bf2);
                Bolder.addChild(bf3);






                    // position the object based on the accumulating variable.
                Bolder.y = currentY;



                Bolder.mouseChildren = false;    // ignore children mouseEvents
                Bolder.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
                Bolder.useHandCursor = true;     // add hand cursor on mouse over
                Bolder.buttonMode = true; 
                trace ('add image with index', k);
                image[k]= new Sprite();

                image[k].addChild(Bolder);

                image.mouseChildren = true;    // ignore children mouseEvents
                image.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
                image.useHandCursor = true;     // add hand cursor on mouse over
                image.buttonMode = true; 
                image[k].addEventListener(MouseEvent.CLICK, gotoscene);

                currentY += Bolder.height + 10;

            }








            if( test.length > 0 )
             {
                _contentHolder = new Sprite();


                addChild(_contentHolder);






       for (var j:int = 0; j < test.length; j++) {



                 _contentHolder.addChild(image[j]);



    } 

     newArray.push(image);


 var viewport:Viewport = new Viewport();

                viewport.y = 0;

                viewport.addChild(_contentHolder);


            var scroller:TouchScroller = new TouchScroller();
            scroller.width = 300;
            scroller.height = 265;
            scroller.x = 10;
    scroller.y = 100;
            scroller.viewport = viewport;
            addChild(scroller);






   }

         function gotoscene(e: MouseEvent):void{
    }

回答1:


removeChild(loading111); tries to remove last created loader icon from “this” class and not from Boulder class object.

You should store your images and other display objects in come container like Vector and access them by id.

But there is rough solution, which may cause you some problems if you will be careless:

function onImageLoaded(e:Event):void {
            DisplayObjectContainer(e.target.loader).parent.removeChildAt(0);

or if it does not work then:

function onImageLoaded(e:Event):void {
    var boulderOfThisImage:DisplayObjectContainer = DisplayObjectContainer(e.target.loader).parent;
    while(boulderOfThisImage.numChildren){
        boulderOfThisImage.removeChildAt(0);
        }
    boulderOfThisImage.addChild(e.target);

I highly disapprove using removeChildAt function. It may cause many problems after changing your display list structure. But it works.



来源:https://stackoverflow.com/questions/23908416/removing-parent-addchild-added-in-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!