Skip to main content
Known Participant
November 18, 2013
Question

Range Error.How can make the movieclip always on the top

  • November 18, 2013
  • 2 replies
  • 1321 views

I want to make the bar on the top at all time but it seems not work when I addChild.  When I add container. it occurs an error.

RangeError: Error #2006: The supplied index is out of bounds.

          at flash.display::DisplayObjectContainer/setChildIndex()

var container:MovieClip=new MovieClip();

addChild(container);

var menubar:MovieClip=new Menubar();

menubar.x=0;

menubar.y=859;

menubar.name="menubar";

container.setChildIndex(menubar,(numChildren-1));

addChild(menubar);

menubar.recipe_btn.addEventListener(MouseEvent.CLICK, onRecipeClick);

function onRecipeClick(evt:MouseEvent):void{

addChildAt(recipe,1);

}

recipe.foodIcon.addEventListener(MouseEvent.CLICK, onFoodIconClick);

function onFoodIconClick(evt:MouseEvent):void{

          removeChild(recipe);

    addChildAt(recipeFood,2);

}

menubar.product_btn.addEventListener(MouseEvent.CLICK, onProductClick);

function onProductClick(evt:MouseEvent):void{

addChildAt(product,3);

}

menubar.cupon_btn.addEventListener(MouseEvent.CLICK, onCuponClick);

function onCuponClick(evt:MouseEvent):void{

          addChildAt(cupon,3);

}

menubar.location_btn.addEventListener(MouseEvent.CLICK, onLocationClick);

function onLocationClick (evt:MouseEvent): void{

          addChildAt(location,3);

}

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
November 18, 2013

You might need to reconsider what your intentions are for the conatiner.  You do not appear to be using it.

container.setChildIndex(menubar,(numChildren-1));

addChild(menubar);

Does not place the menubar inside the container if that is your intention.

If you want to have the menubar remain atop everything else you can just use addChild(menubar) after dealing with everything else you are trying to order.  That will pace it at the top of the display list.

Known Participant
November 18, 2013

That means I need to use addChild(menubar); into each function which change the movieclip?

Ned Murphy
Legend
November 18, 2013

Yes, if you place that as the last line after arranging other content it should keep the menubar on top.

robdillon
Participating Frequently
November 18, 2013

First you need to know where the "bar" is in the displayList stack. You can use this.getChildIndex("bar"); Once you have the position of the bar, you can then use that value to position any new objects that you want to lie below it.  Something like this might work:

var barPosition:int = this.getChildIndex("bar");

menubar.recipe_btn.addEventListener(MouseEvent.CLICK,onRecipeClick);

function onRecipeClick(evt:MouseEvent):void {

     addChildAt(recipe,barPosition-1);

}

Keep in mind that as you add additional items to the display list under the bar object, the bar object's position will change. And so you may need to update the value of that variable.

Known Participant
November 18, 2013

I try to add this code.

var main:MovieClip=new Main();

main.x=0;

main.y=84;

addChild(main);

var menubar:MovieClip=new Menubar();

menubar.x=0;

menubar.y=859;

menubar.name="menubar";

addChild(menubar);

var barPosition:Number= this.getChildIndex("menubar");

menubar.recipe_btn.addEventListener(MouseEvent.CLICK, onRecipeClick);

function onRecipeClick(evt:MouseEvent):void{

          if (currentPage!=null){

                    removeChild(currentPage);

                    removeChild(main);

          }

          addChildAt(recipe,barPosition-1);

          addChildAt(recipe,1);

          currentPage=recipe;

 

}

But it occurs an error

Symbol 'mainpage', Layer 'as', Frame 1, Line 32, Column 441067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject.