所以我使用addChild()成功地从库中添加了一个影片剪辑,但现在我想访问动态添加的影片剪辑中的一些影片剪辑。
我使用了标准点表示法,并且getChildByName也将实例名称传递给它。
我在这里缺少什么?
---- EDITED ----
我尝试了循环的建议并且可以通过索引访问它们,但似乎不是直观的方式来做到这一点......下面是我之前尝试过的两个例子,它们都回来了“ 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();
答案 0 :(得分: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:我希望它有所帮助:)
答案 1 :(得分: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);
}
答案 2 :(得分:1)
您需要引用动态添加的动画片段。然后,如果它具有实例名称,则可以访问特定的子动画片段。
因此,如果你有一个实例名称为'a'的MovieClip,并且你有一个实例名称为'b'的MovieClip,那么你可以简单地将其引用为a.b.但是正如Amarghosh所指出的那样,flash会让你拥有多个具有相同实例名称的动画片段,如果是这样,那么只会访问其中一个动画片段。
答案 3 :(得分: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);
答案 4 :(得分: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));
它看起来像是你刚刚设置的competencyInstance属性,但出于某种原因你通过父进行。我假设您是AS2开发人员学习AS3,因为这是一个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循环迭代通过父方法访问每个child.There我可以说更多关于显示列表及其奇迹,但我想坚持什么属于。
答案 5 :(得分:0)
我能够轻松访问动态加载的库movieClip中的子对象。我实际上做了一个手动循环,根据XML文档中的元素数量创建按钮。调用补间函数,动画完成后,将根据我的x值计算创建并定位新的按钮实例。我在这里做的是一个网站的Flash导航。我想在用户点击按钮时关闭“选择”动画片段,如果用户选择其他按钮,则将其关闭。
你走在正确的轨道上,我基本上使用了你用getChildByName方法做的事情。使用for循环,我能够通过引用创建它们时给出的名称来访问所有动态加载按钮的子项(在本例中为“btn_”加上变量“i。”的值),即“btn_0, btn_1,btn_2“等按钮随后被添加到btnGroup影片剪辑中。所以本质上,我需要做的就是使用getChildByName(“影片剪辑名称”)引用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;
}
}
我希望这会对你有所帮助。
和平