Getting "Error #1006: getChildByName is not a function" when trying to access a text label inside a button object

StackOverflow https://stackoverflow.com/questions/23597604

  •  20-07-2023
  •  | 
  •  

سؤال

I have a button called "SpotButton". I am adding multiple instances of this button to the stage programatically. Here is the code:

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

    }

On the following line:

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

I am trying to set text of a text label used in the button object having instance name as "tooltip_text". But when I compile, I get the following error:

Error #1006: getChildByName is not a function

What am I doing wrong?

هل كانت مفيدة؟

المحلول 3

SimpleButton class does not have getChildByName function. Each state of button can be retrieved as DisplayObjectContainer object. The following code works:

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

نصائح أخرى

Without seeing what is in your SpotButton class it's hard to say, but I would probably guess that you need to cast your mySpot as a DisplayObject.

change your problem line to:

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

Your class needs to inherit from DisplayObjectContainer in order to call getChildByName(). If your SpotButton class inherits from SimpleButton, then it wont be able to call getChildByName() because DisplayObjectContainer isn't in the chain of inheritance:

SpotButton > SimpleButton > InteractiveObject > DisplayObject > EventDispatcher > Object

Unlike Sprite, for example, on which you could call getChildByName():

Sprite  > DisplayObjectContainer > InteractiveObject > DisplayObject > EventDispatcher > Object
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top