dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Changing object's parent

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    4

    resolved [RESOLVED] Changing object's parent

    Hello all,

    I am trying to move a movieclip called Ball from being a child of a ship MC to a child of the stage. This function deposit is a function in ship.as, and screen is a reference to the stage.

    Code:
    public function deposit(ball:Ball):void {
    			this.removeChild(ball);
    			screen.addChild(ball);
    		}
    After some trace calls using contains(), this move was successful, but this code on the main stage that calls the update() function of every child is somehow off.



    Code:
    this.addEventListener(Event.ENTER_FRAME, update);
    function update(e:Event):void {
    	//The following code updates every single movie clip on the screen
    	for(var i = 0; i < stage.numChildren; i++){
    		var mc:Object = getChildAt(i);
    		mc.update();
    	}
    }
    It should be normally iterating through every child, but the moment I push SPACE to detach the ball from the ship, I get this error message:

    RangeError: Error #2006: The supplied index is out of bounds.
    at flash.display::DisplayObjectContainer/getChildAt()

    Given how the for() loop is constructed, how would I ever be calling getChildAt() at an out of bounds i value? How can I fix this error? Thanks!
    Last edited by theeggman85; 09-08-2011 at 12:15 AM.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You don't need to removeChild in order to reparent. The great thing about display lists is that you can pass a subclass of displayobject to any displayobjectcontainer's addChild no matter if it is already on a display list.

    I'm not sure where the update() function exists but you are having it loop over the stages children and calling the getChildAt method of whatever class declares update.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    4
    Aah, I figured out the problem here... I was using stage.numChildren, where stage is the stage property of the main timeline. When I switched to simply numChildren, the count worked perfectly. Thanks for your reply!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center