Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 44 | 1067: Implicit coercion of a value of type String to an unrelated type flash.display:DisplayObject. |
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
That means I need to use addChild(menubar); into each function which change the movieclip?
Copy link to clipboard
Copied
Yes, if you place that as the last line after arranging other content it should keep the menubar on top.
Copy link to clipboard
Copied
Since I am new in as3. If i have two movieclips, I also can use this method. But is it only method for me to organize the movieclips order?
Copy link to clipboard
Copied
There are three ways (that I can readily think of) to control where things get placed in terms of display list indexing...
addChild()
addChildAt()
setChildIndex()
One thing you might need to consider in the way you are dealing with some of the objects is that the lowest index is 0, not 1. The index of the topmost child object will always be one less than the number of children. If you have only one child, its index is 0.
Copy link to clipboard
Copied
before I ask this question, I try to use addChildAt like this. addChildAt(menubar,100); but
The supplied index is out of bounds
This error occurs.
Copy link to clipboard
Copied
As Ned wrote above, the index number is based on the number of items in the displayList. The first item is at index 0, the next item that you add will be at index 1, and so on. You can't use an index number that is out of the range of existing index numbers. So, in order to be able to use the value 100, you would have to already have 101 items in the current displayList.
You can find out how many items are in the displayList at any moment by using containerName.numChildren. So, if you want to know how many items are in the displayList for menubar, you could write:
trace(menubar.numChildren);
Remember that the number of child objects will be one more than the index number for the last child. Indexing starts with 0 and number of children starts with 1.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now