动作脚本3添加儿童和清除第一个孩子

时间:2010-05-25 08:32:04

标签: actionscript-3 addchild

好的,这是设置:

在主时间轴内我有一个菜单,它是一个MovieClip。此菜单由大约17个“按钮”组成,每个按钮指向站点的不同部分。单击“按钮”(2个示例:menu_bag_button,menu_trashbag_button)时,菜单将设置动画,向左移动,2个库项目(bag_container和close_btn)应出现在右侧,两者都在“framecontainer”中。

然后,当菜单在左边时,这个特定的按钮不能重复菜单动画,因此我做了一个if语句查找它所在的帧,如果它在左边,它只是清除framecontainer并加载内容再次。

问题是,当点击另一个链接时,它需要执行以下两项操作之一:如果菜单位于中心且未加载任何内容,则需要为右侧的菜单设置动画,并加载内容/ close_btn进入“framecontainer”;或者,如果菜单在左侧,则需要清除“framecontainer”并加载相应的内容。我已经尝试了很多东西,但由于所有这些按钮都在“menu_mc”中,因此动作脚本必须指向根,然后指向“framecontainer”,所有内容都会变得错综复杂。

如果有一个快速,更好的方法来做到这一点,而我不必花费数小时编码,那就太棒了......否则我现在必须坚持这种错综复杂的方式。

这是我目前的代码。它运行良好,除了它不清除内容的事实,所以它继续加载帧,以便一个关闭,其后还有其他内容:

import flash.display.*;
import fl.transitions.*;
import flash.events.MouseEvent;

//Stopping on this frame (805) so the menu will be in place to interact with
stop();

//framecontainer location/size for the content to be loaded.
//Not sure if the parameters below it are absolutely necessary.
var framecontainer:MovieClip = new MovieClip();
framecontainer.width = 450;
framecontainer.height = 450;
framecontainer.x = 450;
framecontainer.y = 0;

//adding the framecontainer so that the content can be loaded into it.
//again, not sure if this is necessary yet
MovieClip(root).addChild(framecontainer);




//FIRST MENU ITEM, "BAG"
//If we can get the framecontainer to work, I don't think I'll have to create
//a custom closeBtn for each and every link. I'll be able to just use a generic
//closeBtn and have it clear the framecontainer and continue the animation.

var closebagBtn:close_btn = new close_btn();
closebagBtn.x = 0;
closebagBtn.y = 0;

var bagFrame:MovieClip = new bag_frame_mc();
bagFrame.x = 900;
bagFrame.y = 0;


menu_bag_button.addEventListener(MouseEvent.MOUSE_UP, bagClick);
function bagClick(event:MouseEvent):void{
    if(MovieClip(root).currentFrame == 850) {
// the next line does nothing, but it's supposed to clear
MovieClip(root).framecontainer.clear();
}
//Below, I need to add bagFrame and closebagBtn into framecontainer,
//but I don't know how. Tried adding ".framecontainer" in front of addchild
//but got error saying that bagClick (function above) is undefined.
else {
MovieClip(root).addChild (bagFrame);
MovieClip(root).addChild (closebagBtn);
MovieClip(root).gotoAndPlay(806);
}
}
closebagBtn.addEventListener(MouseEvent.MOUSE_UP, bagClose);
function bagClose (event:MouseEvent):void{
MovieClip(root).removeChild(bagFrame);
MovieClip(root).removeChild(closebagBtn);
MovieClip(root).gotoAndPlay(850);
}





//SECOND MENU ITEM, "TRASHBAG"

var closetrashbagBtn:close_btn = new close_btn();
closetrashbagBtn.x = 0;
closetrashbagBtn.y = 0;

var trashbagFrame:MovieClip = new trashbag_frame_mc();
trashbagFrame.x = 900;
trashbagFrame.y = 0;

menu_trashbag_button.addEventListener(MouseEvent.MOUSE_UP, trashbagClick);
function trashbagClick(event:MouseEvent):void{
    if(MovieClip(root).currentFrame == 850) {
// the next line does nothing, but it's supposed to clear
MovieClip(root).framecontainer.clear();
}
//Below, I need to add trashbagFrame and closebagBtn into framecontainer,
//but I don't know how. Tried adding ".framecontainer" in front of addchild
//but got error saying that bagClick (function above) is undefined.
else {
}
else {
MovieClip(root).addChild (trashbagFrame);
MovieClip(root).addChild (closetrashbagBtn);
MovieClip(root).gotoAndPlay(806);
}
}
closetrashbagBtn.addEventListener(MouseEvent.MOUSE_UP, trashbagClose);
function trashbagClose (event:MouseEvent):void{
MovieClip(root).removeChild(trashbagFrame);
MovieClip(root).removeChild(closetrashbagBtn);
MovieClip(root).gotoAndPlay(850);
}

/////////////////////////////////////////////// /////////////// 新的功能代码(需要帮助改进):

import flash.display.*;
import fl.transitions.*;
import flash.events.MouseEvent;

stop();

var menuList:Array = [menu_bag, menu_chips, menu_coke, menu_cup, menu_deodorant, menu_fork, menu_knife, menu_lighter, menu_milk, menu_pill, menu_rings, menu_shampoo, menu_spoon, menu_straw, menu_toothbrush, menu_trashbag, menu_water];



var closeBtn:close_btn = new close_btn();
closeBtn.x = 0;
closeBtn.y = 0;

closeBtn.addEventListener(MouseEvent.MOUSE_UP, contentClose);
function contentClose (event:MouseEvent):void{
    while(MovieClip(root).numChildren > 1)
    {
        MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
    }
MovieClip(root).gotoAndPlay(850);
}




var bagFrame:MovieClip = new bag_frame_mc();
bagFrame.x = 900;
bagFrame.y = 0;

menu_bag.addEventListener(MouseEvent.MOUSE_UP, bagClick);
function bagClick(event:MouseEvent):void{
    if(MovieClip(root).currentFrame == 850) {
    while(MovieClip(root).numChildren > 1)
    {
        MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
    }
MovieClip(root).addChild (bagFrame);
MovieClip(root).addChild (closeBtn);
}
else {
MovieClip(root).addChild (bagFrame);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);
}
}

var trashbagFrame:MovieClip = new trashbag_frame_mc();
trashbagFrame.x = 900;
trashbagFrame.y = 0;

menu_trashbag.addEventListener(MouseEvent.MOUSE_UP, trashbagClick);
function trashbagClick(event:MouseEvent):void{
    if(MovieClip(root).currentFrame == 850) {
    while(MovieClip(root).numChildren > 1)
    {
        MovieClip(root).removeChild(MovieClip(root).getChildAt(MovieClip(root).numChildren - 1));
    }
MovieClip(root).addChild (trashbagFrame);
MovieClip(root).addChild (closeBtn);
}
else {
MovieClip(root).addChild (trashbagFrame);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);
}
}

1 个答案:

答案 0 :(得分:2)

首先,我真的希望这不是“时间轴”代码!我希望这个代码在一个类中。

其次,我真的不喜欢使用“root”的想法。如果您打算多次使用它,您可以为is创建一个新的var,键入为movieclip,并引用它而不是始终将其转换为movieclip。 e.g。

var target:MovieClip = root as MovieClip;
target.addChild(container);

如果此代码在主时间轴上,那么您甚至不需要引用root(但它似乎不是)。

回答你的实际问题......

这些行存在问题:

MovieClip(root).addchild (container);
MovieClip(root).container.addChild (bagLink);
MovieClip(root).addChild (closeBtn);
MovieClip(root).gotoAndPlay(806);

问题在于,当您说 MovieClip(root).addchild(容器); 时,您将容器变量的“内容”添加为root的子级。 var容器的内容没有你称之为“容器”的线索,所以你不能说 MovieClip(root).container

但是你可以说:

container.addChild(bagLink);

由于var容器指向容器MovieClip。