动作如何使用getChildByName获得实例的名称如何使用、实例、名称、动作

2023-09-08 15:03:33 作者:?]"优雅`

好了,所以我有一个影片剪辑名为SC和需要写一个code其中,如果单击按钮(SC),那么会的sC dissapear。该功能需要为多个按钮。我想是

Okay so I have a MovieClip called sC and need to write a code where, if you click the button (sC) then sC will dissapear. The function needs to work for multiple buttons. What I tried was

sC.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
    var self;
    self = MovieClip(getChildByName(event.target.name));
    self.visible=false;

现在,当我尝试这个code,它给了我一个错误,当我点击资深大律师。它说:无法访​​问空对象引用的属性或方法。当我尝试跟踪(个体经营)输出空。有没有一种方法,我可以得到它是使用函数clickhandler功能,然后将对象的实例的名称使这的可见性等于假(可见=假)?

Now when I try this code, it gives me an error when I click sC. It says "cannot access a property or method of a null object reference.". when I try to trace(self) it outputs "null". Is there a way where I can get the name of the instance of the object which is using the clicKHandler function and then make it's visibilty equal to false (visible=false)?

请注意,当我跟踪(event.target.name)它说:instance127。

Note that when I trace(event.target.name) it says "instance127".

推荐答案

在你的code,变量自解析为影片剪辑的名字,而不是完整路径到它的存在。尝试设置它像以下,其中目标是被点击的按钮:

In your code, the variable self resolves to your movieClip's name, but not the complete path to where it exists. Try setting it up like below, where target is the button that was clicked:

sC.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void
{
    event.target.visible = false;
}