1

所以我成功地使用 addChild() 从库中添加了一个影片剪辑,但现在我想访问动态添加的影片剪辑中的一些影片剪辑。

我使用了标准的点表示法,并且 getChildByName 也将实例名称传递给它。

我在这里想念什么?

----已编辑----

我尝试了循环并可以通过索引访问它们的建议,但似乎没有那么直观的方式来做到这一点......下面是我之前尝试过的两个示例,它们都返回为“1119:访问可能未定义的属性 nameText 通过静态类型 flash.display:DisplayObject 的引用。” 或“1061:通过静态类型 flash.display:DisplayObject 的引用调用可能未定义的方法 getChildByName。”

点符号示例:

 //  build circlular display
function buildCircle() {
    trace("buildCircle()");
    if (viewByState == "assignment") {

        var competencyContainer:MovieClip = new MovieClip();
        competencyContainer.name = "competencyContainer";
        this.addChild(competencyContainer);

        var angleSegment:Number = 360 / competenciesArray.length;
        var angleSum:Number = 360 - angleSegment / 2;

        for (var i:Number = 0; i < competenciesArray.length; i++) {

            var competencyInstance:competencyCircle = new competencyCircle();
            competencyInstance.name = "competency" + i;
            competencyContainer.addChild(competencyInstance);


            competencyContainer.getChildByName("competency" + i).x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
            competencyContainer.getChildByName("competency" + i).y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));


            competencyContainer.getChildByName("competency" + i).nameText.wordWrap = true;
            competencyContainer.getChildByName("competency" + i).nameText.embedFonts = true;
            competencyContainer.getChildByName("competency" + i).nameText.htmlText = "COMPETENCY:<br />" + competenciesArray[i].name;

            competencyContainer.getChildByName("competency" + i).nameText.setTextFormat(tfTitle, 0, competencyContainer.getChildByName("competency" + i).nameText.text.length - competenciesArray[i].name.length);

            competencyContainer.getChildByName("competency" + i).nameText.setTextFormat(tfName, competencyContainer.getChildByName("competency" + i).nameText.text.length - competenciesArray[i].name.length, competencyContainer.getChildByName("competency" + i).nameText.text.length);

            competencyContainer.getChildByName("competency" + i).nameText.autoSize = TextFieldAutoSize.CENTER;

            competencyContainer.getChildByName("competency" + i).nameText.y = -(competencyContainer.getChildByName("competency" + i).nameText.height / 2);

            competencyContainer.getChildByName("competency" + i).filters = [circleDefaultDropShadow];
            competencyContainer.getChildByName("competency" + i).selectedIndicator.visible = false;

            competencyContainer.getChildByName("competency" + i).hit.buttonMode = true;
            competencyContainer.getChildByName("competency" + i).hit.mouseEnabled = true;
            competencyContainer.getChildByName("competency" + i).hit.tabEnabled = true;
            competencyContainer.getChildByName("competency" + i).hit.mouseChildren = true;

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleHoverDropShadow];
                                                                                     });

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleDefaultDropShadow];
                                                                                     });

            competencyContainer.getChildByName("competency" + i).hit.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
                                                                                     e.target.parent.filters = [circleDefaultDropShadow];
                                                                                     e.target.parent.selectedIndicator.visible = true;
                                                                                     });

            angleSum -= angleSegment;
            trace("end");
            trace(i);
            trace("\n\n\n");
        }
    } else if (viewByState == "competency") {



    } else {

    }
}
buildCircle();

以及使用 .getChildByName() 的示例:

    //  build circlular display
  function buildCircle() {
    trace("buildCircle()");
    if (viewByState == "assignment") {

        var competencyContainer:MovieClip = new MovieClip();
        competencyContainer.name = "competencyContainer";
        this.addChild(competencyContainer);

        var angleSegment:Number = 360 / competenciesArray.length;
        var angleSum:Number = 360 - angleSegment / 2;

        for (var i:Number = 0; i < competenciesArray.length; i++) {

                var competencyInstance:competencyCircle = new competencyCircle();
                competencyInstance.name = "competency" + i;
                competencyContainer.addChild(competencyInstance);


                competencyContainer.getChildByName("competency" + i).x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
                competencyContainer.getChildByName("competency" + i).y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));


                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").wordWrap = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").embedFonts = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").htmlText = "COMPETENCY:<br />" + competenciesArray[i].name;

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").setTextFormat(tfTitle, 0, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length - competenciesArray[i].name.length);

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").setTextFormat(tfName, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length - competenciesArray[i].name.length, competencyContainer.getChildByName("competency" + i).getChildByName("nameText").text.length);

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").autoSize = TextFieldAutoSize.CENTER;

                competencyContainer.getChildByName("competency" + i).getChildByName("nameText").y = -(competencyContainer.getChildByName("competency" + i).getChildByName("nameText").height / 2);

                competencyContainer.getChildByName("competency" + i).filters = [circleDefaultDropShadow];
                competencyContainer.getChildByName("competency" + i).getChildByName("selectedIndicator").visible = false;

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").buttonMode = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").mouseEnabled = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").tabEnabled = true;
                competencyContainer.getChildByName("competency" + i).getChildByName("hit").mouseChildren = true;

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.MOUSE_OVER, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleHoverDropShadow];
                                                                                                                                                                 });

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.MOUSE_OUT, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleDefaultDropShadow];
                                                                                                                                                                 });

                competencyContainer.getChildByName("competency" + i).getChildByName("hit").addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
                                                                                                                                                                 e.target.parent.filters = [circleDefaultDropShadow];
                                                                                                                                                                 e.target.parent.getChildByName("selectedIndicator").visible = true;
                                                                                                                                                                 });

                angleSum -= angleSegment;
                trace("end");
                trace(i);
                trace("\n\n\n");
        }
    } else if (viewByState == "competency") {



    } else {

    }
}
buildCircle();
4

6 回答 6

3

创建剪辑时如何使用它:

...
competencyInstance.name = "competency" + i;  
competencyContainer.addChild (competencyInstance);          
// this is the additional line, cecessary for accesing later by name
competencyContainer["clipFichaEjemplo2"] = competencyInstance;
...

这是以后访问它们:

...
var M:MovieClip = clipMapa["competency" + i];
...

它在一个类似的 Flash 项目中对我有用,我在动态使用 addChild 时遇到了问题。

ihih:我希望它有帮助:)

于 2010-08-13T04:24:07.063 回答
1

You need to have a reference to the movieclip that was dynamically added. Then you can access a particular child movieclip if it has an instance name.

So if you have MovieClip with instance name 'a' and inside you have a MovieClip with instance name 'b' then you can simply reference it as a.b. But as Amarghosh pointed out flash will let you have multiple movieclips with the same instance names and if thats the case only one of the movieclips will be accessed.

于 2009-09-25T14:34:03.763 回答
1

在 AS3 中,点表示法不像在 AS2 中那样工作。当且仅当您已将子名称显式声明为父对象的变量时,您才能使用它。getChildByName也不可靠,因为没有规定说两个兄弟姐妹不能同名。用于getChildAt循环遍历所有子项。尝试类似:

//assuming newMC as the added movie clip
var num:Number = newMC.numChildren;
for(var i:Number = 0; i < num; i++)
{
  var child:DisplayObject = newMC.getChildAt(i);
  trace(child.name);
}
于 2009-09-25T13:27:42.870 回答
1

你可以试试这样的代码:

//prizma, küp, dortgen, koni, kure, silindir are mc's name
var movieList:Array = [prizma, küp, dortgen, koni, kure, silindir];
var k:Number=Math.round(Math.random()*5);
var nes:MovieClip=new movieList [k] ();
addChild(nes);
于 2016-01-05T21:27:56.120 回答
0

我能够轻松访问动态加载的库movieClip 中的子对象。我实际上做了一个手动循环,它根据 XML 文档中的元素数量创建按钮。调用补间函数,一旦动画完成,就会根据我的 x 值计算创建和定位一个新按钮实例。我在这里制作的是一个网站的 Flash 导航。如果用户单击一个按钮,我想打开“选择”电影剪辑,如果用户选择另一个按钮,我想将其关闭。

你在正确的轨道上,我基本上使用了你在 getChildByName 方法中所做的事情。使用 for 循环,我能够通过引用我在创建它们时给它们的名称(在本例中为“btn_”加上变量“i”的值)来访问所有动态加载的按钮的子项,即“btn_0, btn_1、btn_2" 等。然后将按钮添加到 btnGroup 影片剪辑中。所以本质上,我需要做的就是使用 getChildByName("movie clip name") 引用 btnGroup 的子元素,后跟要从括号中的子元素访问的对象的名称 [""] 形式为细绳。在下面查看我的“鼠标功能”示例以获得正确答案

function btnMaker():void   
{  
  btnGroup.x = 170;  
  if(btnCount < clubXML.btn_group.*.length())  
  {  
    btnCount++;  
    b = new btn();  
    spacer = b.width-27;  
    b.x = (btnCount-1)*spacer;  
    b.y = 25;  
    b.name = "btn_"+(btnCount-1);//Name the dynamicly loaded movie clip  
    b.buttonMode = true;  
    b.mouseChildren = false;  
    b.btn_txt.text = clubXML.btn_group.btn[btnCount-1];  
    b.addEventListener(MouseEvent.CLICK, mouseFunctions);  
    b.addEventListener(MouseEvent.MOUSE_OVER, mouseFunctions);  
    b.addEventListener(MouseEvent.MOUSE_OUT, mouseFunctions);  
    b.grid_mc.stop();  
    b.grid_mc.visible = false;  
    b.select.visible = false;  
    btnGroup.addChild(b);  
    btnGroup.getChildByName("btn_0")["select"].visible = true;   
    tweenIn();  
  }  
  if(btnCount == clubXML.btn_group.*.length())  
  {  
    //btnTimer.stop();  
    xTween.removeEventListener(TweenEvent.MOTION_FINISH, animFinish);  
  }  
}  

function mouseFunctions(e:MouseEvent):void   
{  
  var btnName = e.target.name.substring(4, 5);   

  switch(e.type)   
  {   
    case("mouseOver"):   
      bounceIn = new Tween(e.target, "scaleY", Bounce.easeOut, 1.5, 1, 10, false);  
      e.target.gotoAndPlay(2);   
      e.target.getChildByName("grid_mc").visible = true;   
      //trace(e.target.getChildByName("grid_mc").totalFrames());   
      e.target.getChildByName("grid_mc").play();   
      break;   

    case("mouseOut"):   
      e.target.gotoAndPlay(12);   
      e.target.getChildByName("grid_mc").stop();   
      e.target.getChildByName("grid_mc").visible = false;   
      break;

    case("click"):   
      trace("Button Name: "+btnName);   

      currentSelection = e.target.name;   

      for(var i:int = 0; i < btnGroup.numChildren-1; i++)   
      {   
        btnGroup.getChildByName("btn_"+i)["select"].visible = false;     
      }  

      notIt != currentSelection;   

      if(currentSelection != notIt)   
      {   
        e.target.getChildByName("select").visible = true;   
      }   
      break;   
  }
}

我希望这可以帮助你。

和平

于 2009-10-13T22:11:51.433 回答
0

好的,我已经阅读了您的代码几次,并想指出以下几点。

以下是您的代码片段:

 var competencyInstance:competencyCircle = new competencyCircle();
 competencyInstance.name = "competency" + i;
 competencyContainer.addChild(competencyInstance);

 competencyContainer.getChildByName("competency" + i).x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
 competencyContainer.getChildByName("competency" + i).y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));

在我看来,您只是设置了能力实例的属性,但由于某种原因,您是通过父级进行的。我假设您是学习 AS3 的 AS2 开发人员,因为这是一种 AS2 方法。您已经拥有对该对象的引用,并且不需要访问父对象。

以下是您的代码的外观。

 var competencyInstance:competencyCircle = new competencyCircle();
 competencyInstance.name = "competency" + i;

 competencyInstance.x = circleCenter.x - (Math.sin(angleSum * (Math.PI / 180)) * (circleSize / 2));
 competencyInstance.y = circleCenter.y - (Math.sin((90 - angleSum) * (Math.PI / 180)) * (circleSize / 2));
 competencyContainer.addChild(competencyInstance);

x 和 y 总是相对于父级。

解决这些问题的最好方法是遍历父/容器来跟踪每个对象的名称,您可能会发现您忘记了将事物嵌套得更深,等等。

as3 显示列表是一件很棒的事情,因为您可以通过对象的直接引用来访问对象,而不是通过父级引用的长点语法。唯一需要通过父母访问事物的时候是当您没有参考时。您创建了一个局部变量能力实例,它只能在 buildCircle() 执行时访问,一旦 buildCircle() 完成运行,在其中创建的所有局部变量的生命周期就会结束。话虽如此,当您没有参考时,您将不得不使用 for 循环遍历访问每个子项的父方法。关于显示列表及其奇迹,我还有很多话要说,但我想坚持与什么有关。

于 2009-09-26T18:40:31.380 回答