获取"错误#1006:getChildByName不是函数"尝试访问按钮对象内的文本标签时

时间:2014-05-11 20:32:40

标签: actionscript-3 flash

我有一个名为" SpotButton"的按钮。我以编程方式将此按钮的多个实例添加到舞台。这是代码:

for(var i:int =0; i<myXML.hotspots.*.length(); i++)
    {
        trace(myXML.hotspots.hotspot[i]);

        var mySpot:SpotButton =new SpotButton();

        mySpot.x=myXML.hotspots.hotspot[i].@x;
        mySpot.y=myXML.hotspots.hotspot[i].@y;


        (mySpot.getChildByName("tooltip_text") as TextField).text="being set automatically";


        //mySpot.tooltip_text.text="being set automatically";

        hotspots.push(mySpot);
        hotspotToolTipText.push(mySpot);
    }
for(var j:int =0; j<hotspots.length; j++)
    {
        hotspots[j].addEventListener(MouseEvent.CLICK,hotspotHit);
        hotspots[j].addEventListener(MouseEvent.MOUSE_OVER,hotspotMouseOver(j));
        addChild(hotspots[j]);

    }

在以下行中:

(mySpot.getChildByName("tooltip_text") as TextField).text="being set automatically";

我正在尝试设置实例名称为&#34; tooltip_text&#34;的按钮对象中使用的文本标签的文本。但是当我编译时,我收到以下错误:

  

错误#1006:getChildByName不是函数

我做错了什么?

3 个答案:

答案 0 :(得分:1)

如果没有看到SpotButton课程中的内容很难说,但我可能会猜测您需要将mySpot投射为DisplayObject

将问题行更改为:

(DisplayObject(mySpot).getChildByName("tooltip_text") as TextField).text="being set automatically";

答案 1 :(得分:1)

您的类需要从DisplayObjectContainer继承才能调用getChildByName()。 如果您的SpotButton类继承自SimpleButton,那么它将无法调用getChildByName(),因为DisplayObjectContainer不在继承链中:

SpotButton > SimpleButton > InteractiveObject > DisplayObject > EventDispatcher > Object

与Sprite不同,例如,你可以在其上调用getChildByName():

Sprite  > DisplayObjectContainer > InteractiveObject > DisplayObject > EventDispatcher > Object

答案 2 :(得分:1)

SimpleButton类没有getChildByName函数。可以将每个按钮状态检索为DisplayObjectContainer对象。以下代码有效:

((mySpot.overState as DisplayObjectContainer).getChildAt(1) as TextField).text = "My Custom Text";