Actionscript 3,从库中添加Child,然后通过实例名称访问它的子级

发布于 2024-08-06 00:25:40 字数 9560 浏览 3 评论 0原文

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

我使用了标准点表示法,并且还使用 getChildByName 向其传递实例名称。

我在这里缺少什么?

---- 编辑 ----

我尝试了循环的建议并可以通过索引访问它们,但似乎不是那么直观的方法......下面是我之前尝试过的两个示例它们都以“1119:通过静态类型 flash.display:DisplayObject 的引用访问可能未定义的属性 nameText”返回。或“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();

So I successfully added a movie clip from the library using addChild(), but now I want to access some movieclips that were in that dynamically added movieclip.

I've used standard dot notation and also getChildByName passing it the instance names.

What am I missing here?

---- EDITED ----

I tried the suggestion of looping through and can access them by index, but seems like not as intuitive of a way to do this... below are the two examples of what I had previously tried and they all come back as "1119: Access of possibly undefined property nameText through a reference with static type flash.display:DisplayObject." or "1061: Call to a possibly undefined method getChildByName through a reference with static type flash.display:DisplayObject."

example of dot notation:

 //  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();

and example using .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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

油焖大侠 2024-08-13 00:25:40

创建剪辑时使用它怎么样:

...
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:我希望它有帮助:)

What about using this when create the clips:

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

And this to access them later:

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

it worked for me in a similar flash project where i have the problem using addChild dynamically.

ihih: I hope it helps :)

软甜啾 2024-08-13 00:25:40

您需要引用动态添加的影片剪辑。然后,您可以访问特定的子影片剪辑(如果它有实例名称)。

因此,如果您有实例名称为“a”的 MovieClip,并且内部有一个实例名称为“b”的 MovieClip,那么您可以简单地将其引用为 ab 但正如 Amarghosh 指出的那样,flash 将允许您拥有多个具有相同实例名称的影片剪辑,并且如果在这种情况下,只会访问其中一个影片剪辑。

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.

仅此而已 2024-08-13 00:25:40

在 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);
}

In AS3, dot notation doesn't work the way it used to in AS2. You can use it if and only if you have explicitly declared the child's name as a variable of parent object. getChildByName is also not reliable as there is no rule that says two siblings can't have same name. Use getChildAt to loop through all the children. Try something like:

//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);
}
心如狂蝶 2024-08-13 00:25:40

你可以尝试像这样的代码:

//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);

You can try like this code:

//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);
回忆凄美了谁 2024-08-13 00:25:40

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

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

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;   
  }
}

希望这对您有所帮助。

和平

I was able to easily access the child objects within a dynamically loaded library movieClip. I actually made a manual loop that creates buttons based on the number of elements in an XML document. A tween function is called and once the animation completes, a new button instance is created and positioned according to my x value calculation. What I made here is a Flash navigation for a website. I wanted to switch the "select" movieclip on if the user clicks a button and turn it off if the user selects a different button.

You were on the right track and I essentially used what you were doing with the getChildByName method. With a for loop, I was able to access all of the dynamically loaded button's children by referencing the names I gave them when they were created (in this case "btn_" plus the value of the variable "i.") i.e. "btn_0, btn_1, btn_2" etc. The buttons were then added to the btnGroup movie clip. So in essence, all I needed to do was reference the child elements of btnGroup using getChildByName("movie clip name") followed by the name of the object you want to acces from the child in brackets[""] in the form of a string. View my "Mouse function" example below to get right to the answer

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;   
  }
}

I hope this helps you.

Peace

反话 2024-08-13 00:25:40

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

下面是您的代码片段:

 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));

在我看来,您只是设置了 CompetencyInstance 的属性,但由于某种原因,您是通过父级来设置的。我假设您是一名正在学习 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

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

Ok, I have read through your code a few times and would like to point out the following.

The below is a snippet of your code:

 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));

It looks to me as tho your just setting attributes of competencyInstance, but for some reason your doing it through the parent. I would assume that you are an AS2 developer learning AS3, because this is an AS2 method. You have a reference to the object already and do not need to access the parent.

The following is how your code should look.

 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);

The x and y are always relative to the parent.

The best way to figure these things out, is to iterate through the parent/container tracing out the name of each object, you may find that you forgot that you nested things one deeper, etc.

The as3 display list is a great thing, because you can access objects through their direct reference, instead of the long dot syntax referencing through the parent. The only time you need to access things through the parent as your doing, is when you do not have a reference. You created a local variable competencyInstance which is only access-able while buildCircle() is exectuing, once buildCircle() is finished running, the life of all local variables created within it ends. That all being said, when you don't have a reference, you will have to use the for loop iterate through parent method accessing each child.There is alot more I could say about the display list and its wonders, but I want to stick to what pertains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文